使用复合键加入实体

时间:2011-03-14 23:05:31

标签: jpa hibernate-annotations

我有两个带有复合键的旧数据库实体,其中一个实体有一个带@EmbeddedId注释的复合键。

// 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;
 }

如何定义要加入的@OneToOne关系,如以下示例所示:

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

1 个答案:

答案 0 :(得分:3)

我发现@MapsId注释在我的案例http://download.oracle.com/javaee/6/api/javax/persistence/MapsId.html

中有帮助