指定限制“独一无二”在休眠 [英] Specifying restrictions "unique together" in Hibernate
本文介绍了指定限制“独一无二”在休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个实体,我想指定一个限制,即两个字段应具有唯一的值对。例如。一个字段是所有者,另一个字段是名称,我想限制(所有者,名称)的组合应该是唯一的。但我不想让这些组合主键:
@Entity
@Table(name =keyfile )
public class KeyFile {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private long ID;
@ManyToOne @ForeignKey(name =FK_SIGNATUREID_USER)
私人用户所有者;
@Column(nullable = false,长度= 80)
私有字符串名称;
}
如何用Hibernate注解指定此限制?
解决方案
试试这里提到的解决方案: $ b
https://forum.hibernate.org/viewtopic.php?p=2370666
@Entity
@Table(name =keyfile,
uniqueConstraints = {@UniqueConstraint(columnNames = {owner,name})}
public class KeyFile {...}
I have an entity where I want to specify a restriction that two fields should have a unique pair value. E.g. one field is owner, another is name, I want a restriction that the combination of (owner,name) should be unique. But I do not want to make these a composite primary key:
@Entity
@Table(name="keyfile")
public class KeyFile {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@ManyToOne @ForeignKey(name="FK_SIGNATUREID_USER")
private User owner;
@Column(nullable=false,length=80)
private String name;
}
How do I specify this restriction with a Hibernate annotation?
解决方案
Try the solution mentioned here:
https://forum.hibernate.org/viewtopic.php?p=2370666
Ergo it is
@Entity
@Table(name="keyfile",
uniqueConstraints = {@UniqueConstraint(columnNames={"owner", "name"})}
public class KeyFile { ... }
这篇关于指定限制“独一无二”在休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文