开发者

Can a @OneToOne relationship contain a @ForiegnKey annotation

开发者 https://www.devze.com 2022-12-19 04:16 出处:网络
Does it make sense to have a @OneToOne member contain a @ForiegnKey annotation. @Entity class User { @Id

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

精彩评论

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

关注公众号