2011-04-07 64 views
1

我有两个实体如下:JPA地图非id字段为外键

@Entity 
public class Entity1 
{ 
    @Id 
    Long id; 
    @Basic 
    @OneToOne 
    @Column(unique=true,nullable=false) 
    String awesome; 
...  
} 



and 

@Entity 
public class Entity2 
{ 
    @Id 
    Long id; 
    @OneToOne(mappedBy="awesome",targetEntity=Entity1.class)  
    @Column(name="myAwesome", insertable=false,updateable=false) 
    @Basic 
    String awesome; //FK to Entity1 
}  

我期待生成SQL,看起来像这样:

Alter Table Entity1 Add Constraint Entity1Entity2_Awesome Foreign Key (myAwesome) References Entity1.Awesome 

目前没有SQL是被生成,我使用eclipselink。

回答

0

我想你应该在这里使用@JoinColumn并明确指定连接列的名称。

1

一个字符串不能是OneToOne,你不能将某些东西标记为Basic和OneToOne。

要创建OneToOne关系使用,

@OneToOne ENTITY2 ENTITY2;

所有的关系应该由Id,而不是非真棒等非ID字段。可以在EclipseLink中的非Id字段上定义外键,但不能在JPA中定义外键,因此您需要使用DescriptorCustomizer。

一般情况下,关系应该由Id,所以要么重新考虑Id的关系。