如何在hbm中执行多列UniqueConstraint? [英] How to do multiple column UniqueConstraint in hbm?

查看:179
本文介绍了如何在hbm中执行多列UniqueConstraint?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用一些旧的hibernate代码。

Working on some legacy hibernate code.

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

How do I do the following with hbm.xml(hibernate mapping file) instead of with annotations?

@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>
...

文档摘要:


< properties>元素允许定义一个类的属性的命名,
逻辑分组。最重要的使用
的结构是它允许属性的组合为
的一个property-ref的目标。它也是一个方便的方法来定义
多列唯一约束。

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.

所有可用的选项在< a href =http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-properties =nofollow noreferrer> Hibernate文档。

这篇关于如何在hbm中执行多列UniqueConstraint?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆