本文介绍了集合的唯一约束< String>与休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @ElementCollection 
Set< String>< p> ;分支机构;

现在Hibernate正在创建一个表 project_branches (project_id | branches)并在分支上添加唯一约束。有什么办法可以告诉hibernate:唯一约束应该包含 project_id 分支



我找不到这个问题的答案,但我认为我不是第一个有这个问题的答案......谢谢

解决方案

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



还有另一种配置。请考虑在实体级别添加 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>分支机构;


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. Is there any way to tell hibernate that the unique constraint should involve both, project_id and 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 e.g. below:

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

EDIT: Try using @CollectionTable annotation.

 @ElementCollection
 @CollectionTable(
    name="project_branches",
    joinColumns=@JoinColumn(name="branches"),
    uniqueConstraints = {@UniqueConstraint(columnNames={"project_id", "branches"})}
  )
  Set<String> branches;

这篇关于集合的唯一约束&lt; String&gt;与休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 21:12