我用@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應該工作得很好。 –