我用@OneToOne注释了我的字段,当我检查数据库(使用liquibase生成)时,发现数据库列上存在唯一的约束。@OneToOne是否意味着唯一性?
这是否意味着@OneToOne仅仅意味着唯一性,例如。一座建筑物只能在一个城市中,而其他建筑物不能在同一个城市中?
当我想告诉在同一城市可能有其他建筑物时,我该怎么办?
- 加@JoinColumn(唯一= FALSE),
- 只使用@JoinColumn(唯一= FALSE)没有@OneToOne,
- 或使用@ManyToOne?
- 还是不加任何注释?
我不想在城市类中放置Buildings字段,因为我永远不会打电话给city.getBuildings();.以下任何一项是否需要双向参考?
class Building {
@OneToOne(optional = false)
City city;
}
class Building {
@OneToOne(optional = false)
@JoinColumn(unique = false)
City city;
}
class Building {
@JoinColumn(unique = true)
City city;
}
class Building {
@ManyToOne
City city;
}
单向@ManyToOne应该工作得很好。 –