开发者

Can a @OneToOne relationship contain a @ForiegnKey annotation

Does it make sense to have a @OneToOne member contain a @ForiegnKey annotation.

@Entity 
class User {

    @Id
    @GeneratedValue(strategy = IDENTITY)

    int id;

   @OneToOne
   @ForeignKey
   @JoinColumn(name = "home_address_id", referenc开发者_运维问答edColumnName = "id")
   Address homeAddress;

}

@Entity 
class Address {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    int id;
}


I don't think it is necessary as The @OneToOne mapping should implicitely create a foreign key for the column.

On the other hand, if you think it will help you and other developers to understand the code easier and it doesn't cause any problems, you can leave it there.

However, it seems that the ForeingKey annotation is hibernate specific where the OneToOne annotation is part of the Java Persistence API. This might support the idea of taking it out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜