2

我有一个包含如下集合的实体:

@ElementCollection
Set<String> branches;

现在 Hibernate 所做的是创建一个表project_branches(project_id | 分支) 并在branches. 有没有办法告诉hibernate唯一约束应该同时涉及project_id branches

我找不到这个问题的答案,虽然我认为我不是第一个遇到这个问题的人......谢谢

4

2 回答 2

2

好的,我找到了解决方案:实体字段支持注释@CollectionTable,所以我能够像这样解决它:

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "project_branches", uniqueConstraints = @UniqueConstraint(columnNames = {
        "project_id", "branches" }))
Set<String> branches = new HashSet<String>();
于 2012-10-29T14:57:39.983 回答
2

这取决于映射中涉及的列。

还有另一种配置。UniqueConstraint请考虑在实体级别添加注释,例如:

  @Table(name="project_branches",
    uniqueConstraints = {@UniqueConstraint(columnNames={"project_id", "branches"})}
  )

编辑:尝试使用@CollectionTable注释。

 @ElementCollection
 @CollectionTable(
    name="project_branches",
    joinColumns=@JoinColumn(name="branches"),
    uniqueConstraints = {@UniqueConstraint(columnNames={"project_id", "branches"})}
  )
  Set<String> branches;
于 2012-10-29T14:14:42.623 回答