本文介绍了@UniqueConstraint和@Column(unique = true)在hibernate注释中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@UniqueConstraint @Column(unique = true)

例如:

  @Table(
name =product_serial_group_mask,
uniqueConstraints = {@UniqueConstraint(columnNames = { mask,group})}



b
$ b

  @Column(unique = true)
@ManyToOne(可选= false,fetch = FetchType.EAGER)
私有ProductSerialMask掩码;

@Column(unique = true)
@ManyToOne(可选= false,fetch = FetchType.EAGER)
私人群组;


解决方案

如前所述, Column(unique = true) UniqueConstraint 的快捷方式,只有一个字段。



从你给的例子来看,两者之间有很大的区别。

  @Column(unique = true)
@ManyToOne(可选= false,fetch = FetchType.EAGER)
私有ProductSerialMask掩码;

@Column(unique = true)
@ManyToOne(可选= false,fetch = FetchType.EAGER)
私人群组;

这段代码意味着掩码 group 必须是唯一的,但是是分开的。这意味着,例如,如果您有 mask.id = 1 的记录,并尝试使用 mask.id = 1 插入另一条记录,则会得到一个错误,因为该列应该有唯一的值。

另一方面,

  @Table(
name =product_serial_group_mask,
uniqueConstraints = {@UniqueConstraint(columnNames = {mask,group})}

意味着mask +组合的值应该是唯一的。这意味着您可以拥有 mask.id = 1 group.id = 1 的记录,并且如果您尝试使用插入另一条记录, mask.id = 1 group.id = 2 ,它会成功插入,而在第一种情况下则不会。



如果你想让mask和group都是独立的,并且在类级别是唯一的,那么你必须按如下方式编写代码:

  @Table(
name =product_serial_group_mask,
uniqueConstraints = {
@UniqueConstraint(columnNames =mask),
@UniqueConstraint(columnNames =group)
}

与第一个代码块效果相同。


What is difference between @UniqueConstraint and @Column(unique = true)?

For example:

@Table(
   name = "product_serial_group_mask", 
   uniqueConstraints = {@UniqueConstraint(columnNames = {"mask", "group"})}
)

And

@Column(unique = true)
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private ProductSerialMask mask;

@Column(unique = true)
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private Group group;
解决方案

As said before, @Column(unique = true) is a shortcut to UniqueConstraint when it is only a single field.

From the example you gave, there is a huge difference between both.

@Column(unique = true)
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private ProductSerialMask mask;

@Column(unique = true)
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private Group group;

This code implies that both mask and group have to be unique, but separately. That means that if, for example, you have a record with a mask.id = 1 and tries to insert another record with mask.id = 1, you'll get an error, because that column should have unique values. The same aplies for group.

On the other hand,

@Table(
   name = "product_serial_group_mask", 
   uniqueConstraints = {@UniqueConstraint(columnNames = {"mask", "group"})}
)

Implies that the values of mask + group combined should be unique. That means you can have, for example, a record with mask.id = 1 and group.id = 1, and if you try to insert another record with mask.id = 1 and group.id = 2, it'll be inserted successfully, whereas in the first case it wouldn't.

If you'd like to have both mask and group to be unique separately and to that at class level, you'd have to write the code as following:

@Table(
        name = "product_serial_group_mask",
        uniqueConstraints = {
                @UniqueConstraint(columnNames = "mask"),
                @UniqueConstraint(columnNames = "group")
        }
)

This has the same effect as the first code block.

这篇关于@UniqueConstraint和@Column(unique = true)在hibernate注释中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:56