Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
日食线+;JPA意外生成ID列_Jpa_Eclipselink - Fatal编程技术网

日食线+;JPA意外生成ID列

日食线+;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

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   
    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

谢谢,这正是所需的