Join entity with composite key

冷暖自知 提交于 2019-12-08 10:23:30

问题


I have 2 entities for legacy db with composite keys, one of them has a composite key with @EmbeddedId annotation.

// first entity
@Entity
public class Product {

 @Id
 private Integer productId;

 // lookup table contains code-description pairs
 @OneToOne
 private ProductDefects defects;

 //getters and setters and other code omitted 

}

// lookup entity
@Entity
public class ProductDefects {

 @EmbededId
 private ProductDefectsPK id;

 //getters and setters and other code omitted 

} 

//composite key
@Embedable
 public class ProductDefectsPk{
  private Integer realId;
  private String  category;
 }

How should I define the @OneToOne relation to join as in the following example:

select p.Id, pd.description
from Product p
inner join p.defects pd

回答1:


I figure out that @MapsId annotation helps in my case http://download.oracle.com/javaee/6/api/javax/persistence/MapsId.html



来源:https://stackoverflow.com/questions/5305687/join-entity-with-composite-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!