2011-04-06 83 views
1

的EclipseLink生成建表所示:的EclipseLink JPA +生成ID列意外

Create Table myTable (ID (255) not null, col1 (255), col2(255), col3(255) PK (ID, col1, col2) 



@Embeddable 
MyPK implements Serializable 
{ 
    @OneToOne 
    @Id 
    String col1; 

    @OneToOne 
    @Id 
    String col2;  
... 
} 



@Entity 
MyClass implements Serializable 
{ 
    @EmbeddedId 
    MyPK pk; 
    String col1; 
    String col2; 
    String col3; 
... 
} 

如何防止ID列的产生在Create Table声明?我问,因为em.persist(MyClass)抛出一个约束例外ID为空。我预计@EmbeddedId将覆盖此并防止创建该字段。

编辑
我试图在代码生成表看起来像这样:

fk - col1 
fk - col2 
VarChar - col3 
+0

'String'外键? – 2011-04-06 15:06:06

+0

@Matt是的,的确是我的朋友。 – user123435234234234 2011-04-06 15:09:05

回答

2

第一个问题是一个字符串属性不能是一个OneToOne映射。其次,OneToOne映射不能在EmbeddedId中使用。第三,不要在EmbeddedId中使用@Id注释,因为Embeddable不能拥有标识。

做到这一点,最简单的方法是:

@Entity 
@IdClass(MyPK.class) 
MyClass implements Serializable 
{ 
    @OneToOne 
    @Id 
    TargetClass rel1; 

    @OneToOne 
    @Id 
    SecondTargetClass rel2 

    @Basic 
    String col3; 
... 
} 

MyPK implements Serializable 
{ 
    String rel1; 

    String rel2;  
... 
} 

如果你真的需要一个嵌入式的PK类,然后替换@MapsId的@ID注释,并添加回MyClass中的EmbeddedId注释和嵌入式注释回到MyPK

+0

谢谢你,这正是必要的 – user123435234234234 2011-04-07 15:15:51