日食线+;JPA意外生成ID列
Eclipselink生成了如下create table语句:日食线+;JPA意外生成ID列,jpa,eclipselink,Jpa,Eclipselink,Eclipselink生成了如下create table语句: 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
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;
...
}
如何防止在Create Table
语句中生成ID
列?我这样做是因为em.persist(MyClass)
在ID
为null时抛出一个约束异常。我希望@EmbeddedId
会覆盖此字段并阻止创建该字段
编辑我试图在代码中生成的表如下所示:
fk - col1
fk - col2
VarChar - col3
第一个问题是字符串属性不能是OneToOne映射。第二,OneTONE映射不能在EmbeddedId中使用。第三,不要在EmbeddedId中使用@Id注释,因为EmbeddedId不能具有标识 最简单的方法是:
@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类确实需要一个可嵌入的注释,那么将@ID注释替换为@MapsId,并将EmbeddedId注释添加回MyClass,将EmbeddedId注释添加回MyPK谢谢,这正是所需的