多个@ManyToMany从一个连接表中设置 [英] Multiple @ManyToMany sets from one join table

查看:241
本文介绍了多个@ManyToMany从一个连接表中设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将专有数据库映射到Hibernate以与Spring一起使用。其中有几个连接表,对于实体A和实体B具有以下模式:

  CREATE TABLE AjoinB( 
idA int not null,
idB int not null,
groupEnum enum('groupC','groupD','groupE'),
主键(idA,idB,groupEnum )
);

正如您所看到的,这表明可以有多个A-B关系将它们放入不同的组中。我想结束,实体A的第一行,实体B的第二行,以下集合

 设置< B> ; BforGroupC,BforGroupD,BforGroupE; 
Set< A> AforGroupC,AforGroupD,AforGroupE;

到目前为止,我只设法将它们放在一个集合中,而忽略groupEnum关系属性:

  @ManyToMany(targetEntity = B.class,cascade = {CascadeType.PERSIST,CascadeType.MERGE})
@ JoinTable(name =AjoinB,joinColumns = @ JoinColumn(name =idA),inverseJoinColumns = @ JoinColumn(name =idB))
private Set< B> BforAllGroups;

  @ManyToMany(mappedBy =BforAllGroups,targetEntity = A.class)
private Set< A> AforAllGroups;

如何在groupC,groupD或groupE中创建多个集合?



干杯

Nik

解决方案

如果您考虑这样做,则不要。现在的桌子便宜,所有的经济都是如此,所以每个协会都要创建一个桌子。它会容易得多。



如果您受到旧数据库的约束,并且您无法更改该表的结构,那么我会


  1. 首先考虑skaffman的解决方案(+1,btw)。根据您的目标数据库,您可能可以为您的视图编写触发器,以插入适当的鉴别器值。

  2. 如果上述不可行在你的数据库中,另一个解决方案是使用用于CRUD操作的自定义SQL 。请记住,对于作为条件一部分的涉及关联的复杂HQL查询,这将 NOT 工作(例如,您的鉴别值不会被应用)。您也可以在上面混合/匹配 - 例如使用视图并使用自定义SQL进行插入/删除。 如果上述两种情况均失败,请按照framer8的建议与关联作为单独的实体一起使用。由于组合键和所有无关的代码,这将是相当丑陋的(因为我们假设你不能更改你的表)。事实上,如果您的任何关联允许重复,这可能是不可能的。


I'm mapping a proprietary database to Hibernate for use with Spring. In it, there are a couple of jointables that, for entity A and entity B have the following schema:

CREATE TABLE AjoinB (
  idA int not null,
  idB int not null,
  groupEnum enum ('groupC', 'groupD', 'groupE'),
  primary key(idA, idB, groupEnum)
);

As you can see, this indicates that there can be multiple A-B relationships that put them in different groups. I'd like to end up with, first line for entity A and second for entity B, the following sets

Set<B> BforGroupC, BforGroupD, BforGroupE;
Set<A> AforGroupC, AforGroupD, AforGroupE;

So far, I've only managed to put them in one set and disregard the groupEnum relationship attribute:

@ManyToMany(targetEntity=B.class, cascade={ CascadeType.PERSIST, CascadeType.MERGE } )
@JoinTable(name="AjoinB", joinColumns=@JoinColumn(name="idA"), inverseJoinColumns=@JoinColumn(name="idB") )
private Set<B> BforAllGroups;

and

@ManyToMany( mappedBy = "BforAllGroups", targetEntity = A.class )
private Set<A> AforAllGroups;

How can I make multiple sets where they belong either in groupC, groupD or groupE?

Cheers

Nik

解决方案

If you're considering doing this, don't. Tables are cheap nowadays what's with the economy and all, so just create one per association; it'll be so much easier.

If you're bound by a legacy database and you can't change the structure of that table I would

  1. Consider skaffman's solution first (+1, btw). Depending on your target database you may be able to write a trigger for your views that would insert adequate "discriminator" value.

  2. If the above isn't possible in your DB, another solution is to use custom SQL for CRUD operations for your collections. Keep in mind that this will NOT work (e.g. your "discriminator value" won't get applied) for complex HQL queries involving your association as part of condition. You can also mix / match this with above - e.g. use views and use custom SQL for insert / delete.

  3. If both of the above fail, go with "association as a separate entity" as suggested by framer8. That's going to be rather ugly (since we're assuming here you can't change your tables) due to composite keys and all extraneous code. It may, in fact, be impossible if any of your associations allows duplicates.

这篇关于多个@ManyToMany从一个连接表中设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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