对于这种情况,如何在休眠中定义唯一约束?
数据库级别的 Destination 和 DestinationAlias 如下所示:
在 DestinationAlias 中,(idDestination, alias) 是唯一的。
DestinationAlias 的 POJO:
@Entity
@Table(name = "DESTINATIONALIAS",
uniqueConstraints = { @UniqueConstraint(columnNames={"IDDESTINATION", "ALIAS"}) }
)
public final class DestinationAlias {
// ..
@ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
@JoinColumn(name="IDDESTINATION", nullable=false)
public Destination getMainCity() {
return mainCity;
}
}
唯一约束应该是 IDDESTINATION 还是“MAINCITY”?
Destination and DestinationAlias at database level look like this:
In DestinationAlias, (idDestination, alias) is unique.
POJO for DestinationAlias:
@Entity
@Table(name = "DESTINATIONALIAS",
uniqueConstraints = { @UniqueConstraint(columnNames={"IDDESTINATION", "ALIAS"}) }
)
public final class DestinationAlias {
// ..
@ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
@JoinColumn(name="IDDESTINATION", nullable=false)
public Destination getMainCity() {
return mainCity;
}
}
Should the unique constraint be IDDESTINATION or "MAINCITY" ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
唯一约束注释需要一个列名列表。所以就你而言,不,它不应该是“MAINCITY”。另请注意,最佳实践是使用与实际数据库字段相同的大小写,因为某些数据库区分大小写。所以你的代码应该是:
请参阅: http://docs.oracle .com/javaee/6/api/javax/persistence/UniqueConstraint.html
The unique constraint annotation expects a list of column names. So in your case, no, it should not be 'MAINCITY'. Also note thats its best practice to use the same case as your actual database fields, as some databases are case sensitive. So your code should be:
See: http://docs.oracle.com/javaee/6/api/javax/persistence/UniqueConstraint.html