Hibernate / JPA多对多的关系通过一个连接表和一个复合关键字Unique Constraint问题 [英] Hibernate / JPA many to many relationship through a join table and a composite key, Unique Constraint issue

查看:107
本文介绍了Hibernate / JPA多对多的关系通过一个连接表和一个复合关键字Unique Constraint问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我昨天问了这个问题,但是目标帖子已经改变了,问题也不同了:

Hibernate / JPA元素与多对多关系的集合?



我想知道是否可以创建将建立我所需关系的实体,以便在启动我的应用程序时Hibernate将创建我的模式。



我想要的关系如下所示:

1 http://a8.sphotos.ak.fbcdn.net/hphotos-ak-ash4/417593_10150594114269218_505554217_8657377_1475865815_n.jpg



事情是,Join表可以实际包含不链接到任何元素的行。该结构表示基于类型和值对对元素进行分类,并输入到此特定应用程序之外的系统中。



我想要能够做的是通过映射设置我的元素Hibernate实体包含类别列表,以便我可以实际看到我的元素属于哪个类别,以便hibernate为我创建表格。



这是我到目前为止所做的:将这个映射到我的Element Entity类中,如下所示:

  @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(){
返回类别;
}

这是我想要的大部分,它创建了我的表,我希望他们如何禁止一件事,在(类型,值)对的元素表中添加了一个唯一约束。我不希望这样做,因为多个元素可以具有相同的类型和值对,我需要能够停止开始创建唯一约束,但无法弄清楚如何使用当前映射,我可以这样做吗?我错过了多对多关系的关键吗?

解决方案

实际上,Hibernate在类型和值列。



您在@ManyToMany映射中说,在连接表中,连接列是类型和值列。所以基本上你说hibernate应该通过value和type属性来确定哪个元素与ElementCategory耦合。因此这两个属性的组合应该是唯一的。否则hibernate将不知道哪个元素属于哪个元素类型

如果您希望多个元素实体可以耦合到多个元素类型实体,并且类型和值的组合不总是唯一的,那么你不能使用这些属性作为连接列


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:

1 http://a8.sphotos.ak.fbcdn.net/hphotos-ak-ash4/417593_10150594114269218_505554217_8657377_1475865815_n.jpg

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?

解决方案

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

这篇关于Hibernate / JPA多对多的关系通过一个连接表和一个复合关键字Unique Constraint问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆