Hibernate/JPA通过连接表和组合键实现多对多关系,唯一约束问题
所以我昨天问了这个问题,但是目标帖子已经改变,问题也不同了:
我想知道是否可以创建对我所需的关系进行建模的实体,以便 Hibernate 在以下情况下创建我的架构:我启动我的应用程序。
我想要的关系如下所示:
问题是,Join 表实际上可以包含不链接到任何元素的行。该结构表示基于“类型”和“值”对的元素分类,并被输入到该特定应用程序之外的系统中。
我希望能够做的是通过映射将我的元素 Hibernate 实体设置为包含类别列表,以便我实际上可以看到我的元素所属的类别,并且让 hibernate 为我创建表。
这是我到目前为止所得到的:在我的元素实体类中映射它,如下所示:
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "ELEMENT_ELEMENTCATOGORY", joinColumns = {
@JoinColumn(name = "type", referencedColumnName = "type"),
@JoinColumn(name = "value", referencedColumnName = "value") })
@Column(name = "category")
public List<ElementCategory> getCategories() {
return categories;
}
这完成了我想要的大部分操作,它创建了我的表,如上所述,正是我想要的,除了一件事之外,添加了一个唯一约束(类型,值)对上的元素表。我不希望这样,因为多个元素可以具有相同的类型和值对,我需要能够从创建开始停止唯一约束,但无法弄清楚如何使用当前映射,我可以这样做吗?我是否忽略了多对多关系的要点?
So I asked this question yesterday, but the goal posts have changed and the question is different:
Hibernate / JPA Collection of Elements with Many to Many relationship?
I want to know if it's possible to create entities that will model my required relationship so that Hibernate will create my schema when I fire up my application.
The relationship I want looks like this:
The thing is that the Join table can actually contain rows that don't link to any Elements. The structure represents categorising of elements based on the "type" and "value" pair and are entered in to the system outside of this particular application.
What I would like to be able to do is set my Element Hibernate Entity to contain a list of Categories, via a mapping, so that I can actually see what Categories my element belongs to AND so that hibernate creates the table for me.
Here's what I've got so far:mapping this in my Element Entity class like this:
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "ELEMENT_ELEMENTCATOGORY", joinColumns = {
@JoinColumn(name = "type", referencedColumnName = "type"),
@JoinColumn(name = "value", referencedColumnName = "value") })
@Column(name = "category")
public List<ElementCategory> getCategories() {
return categories;
}
This does most of what I want, it creates my tables as above, exactly how I want them bar one thing, there's a Unique Constraint added in to the Element Table on the (type,value) pair. I don't want this because multiple elements can have the same type and value pair, I need to be able to stop the Unique Constraint from begin created, but can't figure out how with the current mapping, can I do this? Am I missing the point of a Many to Many relationship?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Hibernate 对类型和值列设置了唯一的约束,这实际上看起来很合乎逻辑。
您在 @ManyToMany 映射中说,在可连接中,连接列是类型和值列。所以基本上你说 hibernate 应该通过 value 和 type 属性来确定哪个元素耦合到 ElementCategory。因此这两个属性的组合应该是唯一的。否则hibernate将不知道哪个Element属于哪个ElementType
如果您希望多个Element实体可以耦合到多个ElementType实体并且类型和值的组合并不总是唯一的,那么您不能将这些属性用作joincolumns
It actually seems quite logical that Hibernate puts a unique constraint on the type and value column.
You say in the @ManyToMany mapping that in the jointable the joincolumns are the type and value column. So basically you say that hibernate should determine which element is coupled to the ElementCategory by the value and type property. therefore the combination ofthose 2 properties should be unique. Otherwise hibernate would not know which Element belong to what ElementType
If you want that multiple Element entities can be coupled to multiple ElementType Entities and the combination of type and value is not always unique, then you can't use those properties as joincolumns
您可以指定 unique = false ,我指定的方式对我有用
You can specify unique = false , the way i have specified, worked for me