如何在hbm中进行多列UniqueConstraint?

发布于 2024-08-31 11:16:51 字数 464 浏览 1 评论 0原文

处理一些遗留的休眠代码。

如何使用 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;
}

Working on some legacy hibernate code.

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘和柠 2024-09-07 11:16:51

使用 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>
...

文档摘录:

<属性>元素允许定义一个命名的,
类的属性的逻辑分组。最重要的用途
该构造的特点是它允许属性的组合
property-ref 的目标。这也是定义
多列唯一约束。

所有可用选项均在 中描述Hibernate 文档

Use the properties tag:

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

Documentation extract :

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.

All available options are described in the Hibernate documentation.

冷血 2024-09-07 11:16:51

您也可以这样做:

  <many-to-one name="client" unique-key="uk1,uk2" .../>
  <property name="username" unique-key="uk1"  .../>
  <property name="email" unique-key="uk2"  .../>

您不需要使用 hbm 中的标签。
如果您只想要多个唯一约束。

You can also do this:

  <many-to-one name="client" unique-key="uk1,uk2" .../>
  <property name="username" unique-key="uk1"  .../>
  <property name="email" unique-key="uk2"  .../>

You do not need to use the tag in hbm .
If you only want multiple unique constraints.

蓝礼 2024-09-07 11:16:51

您可以将相同的唯一键属性添加到两个不同的列。这将创建复合唯一密钥。

<property name="firstName" column="first_name" unique-key="name" />
<property name="lastName" column="last_name" unique-key="name" />

在上面的示例中,将从first_name 和last_name 列创建唯一键。

You can add same unique-key attribute to two different columns. This will create composite unique key.

<property name="firstName" column="first_name" unique-key="name" />
<property name="lastName" column="last_name" unique-key="name" />

In the above example the unique key will be created from both first_name and last_name column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文