Spring JPA加入两个实体

时间:2018-08-21 06:53:22

标签: spring jpa entity

我正在努力使用jpa crudRepository接口连接两个实体模型。我不知道如何映射两个实体模型并在@Query批注内编写查询。这些是我的实体类。 我想执行此查询“ SELECT dppd.payment_plan_id,dppd.attribute_value,dppd.attribute_id FROM taxi_driver_mapping AS tdm JOIN driver_payment_plan_details AS dppd on dppd.payment_plan”

@Getter
@Setter
@Entity
@ToString
@Table(name = "driver_payment_plan_details")
@NoArgsConstructor
public class DriverPaymentPlanDetails
{
@Id
@Column(name = "id")
private int id ;

@Column(name = "payment_plan_id")
private long paymentPlanId;

@Column(name = "attribute_id")
private int attributeId;

@Column(name = "attribute_value")
private float attributeValue;

}

@Getter
@Setter
@ToString
@Table(name = "taxi_driver_mapping")
@Entity
@NoArgsConstructor
public class TaxiDriverMapping
{
@Column(name = "mapping_id")
@Id
private Long mappingId;

@Column(name = "mapping_driverid")
private Long mappingDriverId;

@Column(name = "mapping_taxi_model_id")
private String mappingTaxiModelId;

@Column(name = "mapping_status")
private String mappingStatus;

@Column(name = "mapping_payment_plan_id")
private Long mappingPaymentPlanId;

}

1 个答案:

答案 0 :(得分:0)

谢谢你们显示正确的路径,这是我的代码。

@Getter
@Setter
@ToString
@Table(name = "taxi_driver_mapping")
@Entity
@NoArgsConstructor
public class TaxiDriverMapping
{
@Column(name = "mapping_id")
@Id
private Long mappingId;

@Column(name = "mapping_driverid")
private Long mappingDriverId;

@Column(name = "mapping_taxi_model_id")
private String mappingTaxiModelId;

@Column(name = "mapping_status")
private String mappingStatus;

@Column(name = "mapping_payment_plan_id")
private Long mappingPaymentPlanId;

@OneToMany(mappedBy = "taxiDriverMapping")
private List<DriverPaymentPlanDetails> driverPaymentPlanDetails;

}

 @Getter
 @Setter
 @Entity
 @ToString
 @Table(name = "driver_payment_plan_details")
 @NoArgsConstructor
 public class DriverPaymentPlanDetails
 {
@Id
@Column(name = "id")
private int id ;

@Column(name = "payment_plan_id")
private long paymentPlanId;

@Column(name = "attribute_id")
private int attributeId;

@Column(name = "attribute_value")
private float attributeValue;

@ManyToOne()
@JoinColumn(name="payment_plan_id",insertable = false,updatable = false)
private TaxiDriverMapping taxiDriverMapping;

}