Java 正确映射JPA实体

Java 正确映射JPA实体,java,hibernate,mapping,Java,Hibernate,Mapping,我有一个查询,当通过MySQL workbench运行时,它会给出预期的结果 select * from route_generations JOIN linked_customers on linked_customers.linked_customer_id = route_generations.customer_id; 然后我有两个实体/java类,如下所示: @Entity @Table(name = "route_generations") public class RouteGe

我有一个查询,当通过MySQL workbench运行时,它会给出预期的结果

select * from route_generations JOIN linked_customers on linked_customers.linked_customer_id = route_generations.customer_id;
然后我有两个实体/java类,如下所示:

@Entity
@Table(name = "route_generations")
public class RouteGeneration {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
private String customerName;

@NotNull
private String numDevices;

@NotNull
private LocalDateTime startDae;

private LocalDateTime endDate;

@NotNull
private double latitude;

@NotNull
private double longitude;

@NotNull
private int userId;

private long linkedId;

@NotNull
private String customerId;

public String getCustomerId() {
    return customerId;
}

public void setCustomerId(String customerId) {
    this.customerId = customerId;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getCustomerName() {
    return customerName;
}

public void setCustomerName(String customerName) {
    this.customerName = customerName;
}

public String getNumDevices() {
    return numDevices;
}

public void setNumDevices(String numDevices) {
    this.numDevices = numDevices;
}

public LocalDateTime getStartDae() {
    return startDae;
}

public void setStartDae(LocalDateTime startDae) {
    this.startDae = startDae;
}

public LocalDateTime getEndDate() {
    return endDate;
}

public void setEndDate(LocalDateTime endDate) {
    this.endDate = endDate;
}

public double getLatitude() {
    return latitude;
}

public void setLatitude(double latitude) {
    this.latitude = latitude;
}

public double getLongitude() {
    return longitude;
}

public void setLongitude(double longitude) {
    this.longitude = longitude;
}

public int getUserId() {
    return userId;
}

public void setUserId(int userId) {
    this.userId = userId;
}

public long getLinkedId() {
    return linkedId;
}

public void setLinkedId(long linkedId) {
    this.linkedId = linkedId;
}
下一节课:

@Entity
@Table(name = "linked_customers")
public class KinesisLinkedCustomer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long linkedId;

@NotNull
private String linkedServiceId;

@NotNull
private String linkedCustomerId;

@NotNull
private String imei;

@NotNull
private long userId;

public long getLinkedId() {
    return linkedId;
}

public void setLinkedId(long linkedId) {
    this.linkedId = linkedId;
}

public String getLinkedServiceId() {
    return linkedServiceId;
}

public void setLinkedServiceId(String linkedServiceId) {
    this.linkedServiceId = linkedServiceId;
}

public String getLinkedCustomerId() {
    return linkedCustomerId;
}

public void setLinkedCustomerId(String linkedCustomerId) {
    this.linkedCustomerId = linkedCustomerId;
}

public String getImei() {
    return imei;
}

public void setImei(String imei) {
    this.imei = imei;
}

public long getUserId() {
    return userId;
}

public void setUserId(long userId) {
    this.userId = userId;
}
然后,我在JPARepository中通过以下定制方法查询数据库:

@Repository
public interface RouteGenerationRepository extends JpaRepository<RouteGeneration, Long> {

@Query("FROM route_generations JOIN linked_customers on linked_customers.linked_customer_id = route_generations.customer_id")
Page<RouteGeneration> getAllLinkedCustomers(Pageable pageable);

任何帮助都将不胜感激

这是通过在my Repository类中使用以下查询解决的:

FROM RouteGeneration r JOIN KinesisLinkedCustomer kl on kl.linkedCustomerId = r.customerId

对于任何未来的读者,请始终记住使用java字段名而不是数据库列名

是的,我认为您应该通过OneToMany注释,然后您可以查询您的数据库,而无需wirtting纯SLQ。
org.hibernate.hql.internal.ast.QuerySyntaxException:route\u generations未映射
在查询中使用类名而不是表名
来自RouteGeneration…
来自RouteGeneration在KinesisLinkedCustomer.linked_customer_id=RouteGeneration.customer_id上加入KinesisLinkedCustomer我现在得到这个错误:org.hibernate.hql.internal.ast.QuerySyntaxException:null[从com.example.polls.model.RouteGeneration在KinesisLinkedCustomer.linked_customer_id=RouteGeneration.customer_id上加入com.example.polls.model.kineslinkedcustomer
@OneToMany on the field linked_customer_id in type linked customers
FROM RouteGeneration r JOIN KinesisLinkedCustomer kl on kl.linkedCustomerId = r.customerId