简体   繁体   English

一个连接表中有多个@ManyToMany集

[英]Multiple @ManyToMany sets from one join table

I'm mapping a proprietary database to Hibernate for use with Spring. 我正在将专有数据库映射到Hibernate以便与Spring一起使用。 In it, there are a couple of jointables that, for entity A and entity B have the following schema: 其中,对于实体A和实体B,有几个可折叠对象具有以下架构:

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 AB relationships that put them in different groups. 如您所见,这表明可能存在多个AB关系,将它们置于不同的组中。 I'd like to end up with, first line for entity A and second for entity B, the following sets 我想结束的是,对于实体A,第二行对于实体B,以下几组

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: 到目前为止,我仅设法将它们放在一个集合中,而无视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;

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? 如何在groupC,groupD或groupE中制作它们所属的多个集合?

Cheers 干杯

Nik 尼克

To my knowledge, Hibernate cannot use such a "discriminator" column in the way that you want. 据我所知,Hibernate无法按照您想要的方式使用这样的“ discriminator”列。 Hibernate requires a join table for each of them. Hibernate需要为每个连接表。

Perhaps you might be able to define additional views on the table, showing each of the groupings? 也许您可以在表上定义其他视图,以显示每个分组?

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). 首先考虑斯卡夫曼的解(+1,顺便说一句)。 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. 如果无法在数据库中实现上述功能,则另一种解决方案是对集合使用自定义SQL进行CRUD操作 Keep in mind that this will NOT work (eg your "discriminator value" won't get applied) for complex HQL queries involving your association as part of condition. 请记住,这是不行的(例如,将不会应用你的“鉴别值”)为涉及您的关联作为条件的一部分复杂HQL查询。 You can also mix / match this with above - eg use views and use custom SQL for insert / delete. 您也可以将其与上面的内容混合/匹配-例如,使用视图并使用自定义SQL进行插入/删除。

  3. If both of the above fail, go with "association as a separate entity" as suggested by framer8. 如果以上两种方法均失败,请使用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. 如果您的任何关联都允许重复,则实际上是不可能的。

I think the advise anytime you need to access a field in a link table is to make the link table an object and a hibernate entity in its own right. 我认为建议您随时需要访问链接表中的字段的建议是,使链接表本身成为对象和休眠实体。 A would have a set of AtoB objects and AtoB would have a set of B objects. A将具有一组AtoB对象,而AtoB将具有一组B对象。 I have a simmilar situation where the link table has a user associated with the link. 我有一个类似的情况,其中链接表有一个与链接关联的用户。

select joinTable.b from A a 
left join a.AtoB joinTable
where joinTable.group = 'C'

It's not as elegant as having an implicit join done by hibernate, but it does give you the control you need. 它不像通过hibernate进行隐式连接那样优雅,但是它确实可以为您提供所需的控制权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM