hibernate - 如何在 hbm 中做多列 UniqueConstraint?

标签 hibernate persistence constraints unique-constraint hibernate-mapping

处理一些遗留的 hibernate 代码。

如何使用 hbm.xml( hibernate 映射文件)而不是注释来执行以下操作?

@Table(name="users", uniqueConstraints = {
    @UniqueConstraint(columnNames={"username", "client"}),
    @UniqueConstraint(columnNames={"email", "client"})
})
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    private int id;
    private String username;
    private String email;
    private Client client;
}

最佳答案

使用 properties标签:

...
<properties name="uk1" unique="true">
        <property name="username" .../>
        <many-to-one name="client" .../>
</properties>

<properties name="uk2" unique="true">
        <property name="email" .../>
        <many-to-one name="client" update="false" insert="false" .../>
</properties>
...

文档摘录:

The <properties> element allows the definition of a named, logical grouping of the properties of a class. The most important use of the construct is that it allows a combination of properties to be the target of a property-ref. It is also a convenient way to define a multi-column unique constraint.



Hibernate documentation 中描述了所有可用的选项.

关于hibernate - 如何在 hbm 中做多列 UniqueConstraint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2741088/

相关文章:

java - org.hibernate.exception.SQLGrammarException : could not execute statement

Java JPA Hibernate SqlResultSetMapping 别名

hibernate - 在 Grails/Tomcat 上的长批处理期间出现 OutOfMemoryError

java - 将 PostgreSQL ltree 类型与 Java ORM 层一起使用

mysql - 在 MySQL 中,更改主键的模式是否会影响其他表中外键的模式?

java - Hibernate - 非工作代码以及什么是服务注册表

ios - 我从 NSString 的 'componentsSeparatedByString' 方法得到了奇怪的输出

java - Spring data PESSIMISTIC_WRITE 返回旧的 DB 值

iOS:在所有屏幕上显示相同质量的图像

swift - 检查约束是否满足