在另一个表中使用复合外键映射列数据

时间:2019-01-15 13:46:17

标签: java hibernate jpa mapping composite-key

我有两个休眠映射,

@Entity
@Table(name = "PRODUCT_INFO")
public class Product  implements Serializable, Persistable<CustomerPK> {

      @EmbedableId
      private ProductPK cpk;
      @Column(name="tokenNo")
      private String tokenNo; //Getters and setters as usual

}

CustomerPK.java

 @Embeddable
 public class ProductPK implements Serializable{ 

  private String companyId;
  private String productId; //With these two columns I have created PK for PRoduct


 }

现在我想将tokenNo表中的Product映射到另一个Customer表中。

  @Entity
  @Table(name = "CUSTOMER_INFO")
  public class Customer  implements Serializable, Persistable<CustomerPK> {

    @Column(name="product_id")
    private String productId; 
    @Column(name="companyId")
    private String companyId;


    private String tokenNo; // I want to map this column in Product column with companyId+productId
  }

我需要一种方法,以(companyId + productId )类中的外键Product映射该列。

无论如何,请在Spring JPA Hibernate中提供帮助。

摘要(以JDBC表示):

  

当我查询Customer实体时,我想在tokenNo列中添加数据(通过从Product表中以productId + companyId组合进行获取)。   如果通常不是休眠的话,我应该分别查询产品表,以获取tokenNo表中给定productIdcompanyId的{​​{1}}。

有什么办法可以做到,而无需在Hibernate和Spring JPA中进行上述操作?

0 个答案:

没有答案