[英]Unique Constraint for Set<String> with Hibernate
I'm having an Entity that contains a Set like the following: 我有一个包含如下所示的集合的实体:
@ElementCollection
Set<String> branches;
Now what Hibernate does, is creating a table project_branches
(project_id | branches) and adding a unique constraint on branches
. 现在Hibernate所做的是创建一个表
project_branches
(project_id | branches)并在branches
上添加一个唯一约束。 Is there any way to tell hibernate that the unique constraint should involve both, project_id
and branches
? 有没有办法告诉hibernate唯一约束应该涉及
project_id
和 branches
?
I could not find an answer to this question, though I think I'm not the first with this problem... Thanks 我找不到这个问题的答案,虽然我认为我不是第一个遇到这个问题的人...谢谢
This is dependent on the columns involved in the mapping. 这取决于映射中涉及的列。
There in one other configuration. 还有一种配置。 Please consider adding an
UniqueConstraint
annotation at entity level eg below: 请考虑在实体级别添加
UniqueConstraint
注释,例如:
@Table(name="project_branches",
uniqueConstraints = {@UniqueConstraint(columnNames={"project_id", "branches"})}
)
EDIT: Try using @CollectionTable
annotation. 编辑:尝试使用
@CollectionTable
注释。
@ElementCollection
@CollectionTable(
name="project_branches",
joinColumns=@JoinColumn(name="branches"),
uniqueConstraints = {@UniqueConstraint(columnNames={"project_id", "branches"})}
)
Set<String> branches;
Ok I found the solution: Entity fields support the Annotation @CollectionTable
so I was able to solve it like this: 好的,我找到了解决方案:实体字段支持Annotation
@CollectionTable
所以我能够像这样解决它:
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "project_branches", uniqueConstraints = @UniqueConstraint(columnNames = {
"project_id", "branches" }))
Set<String> branches = new HashSet<String>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.