简体   繁体   English

如何在多对多关系中使用Hibernate在连接表中插入常量值?

[英]How to insert constant value in a join table with Hibernate in a many-to-many relation?

I have a mapping like A - AB - B but the AB table is also a join table for other tables (yeah, thanks, no comment :p). 我有一个像A - AB - B的映射,但AB表也是其他表的连接表(是的,谢谢,没有评论:p)。 In the AB table, I have a CODE (non-null) column. 在AB表中,我有一个CODE(非空)列。

I can fetch the correct datas by adding a where tag in the mapping, but when I insert values, Hibernate does not add the value in the CODE column... Well, I don't know how to tell Hibernate to do that. 我可以通过在映射中添加where标签来获取正确的数据,但是当我插入值时,Hibernate不会在CODE列中添加值...好吧,我不知道如何告诉Hibernate这样做。

Here is the mapping of the A table to get a Set: 以下是A表的映射以获取Set:

      <set name="b" table="AB" lazy="false" where="CODE='1.2.3'">
        <key column="A_ID" />
          <many-to-many column="B_ID" class="B">
          </many-to-many>
      </set>

Hibernate create an insert with only the A_ID and the B_ID values, I'd like to tell Hibernate to insert CODE='1.2.3' in its SQL INSERT query. Hibernate创建一个只有A_ID和B_ID值的插入,我想告诉Hibernate在其SQL INSERT查询中插入CODE ='1.2.3'。

Many thanks for your help, 非常感谢您的帮助,

UPDATE UPDATE

The idea in a Java point of view is to have a signature like this in A: getB():Set<B> I do not want getAB():Set<AB> . 从Java的角度来看,这个想法是在A: getB():Set<B>有这样的签名getB():Set<B>想要getAB():Set<AB>

Thanks 谢谢

for AB use two classes one for ID s ABIdClass(A_ID,B_ID) and the other for Code (and all extra columns) ABClass(CODE) and use default attribute of column for constant value: 对于AB使用两个类,一个用于ID s ABIdClass(A_ID,B_ID) ,另一个用于Code (和所有额外列) ABClass(CODE)并使用列的default属性作为常量值:

<class name="ABClass" table="AB">
    <composite-id name="id" class="ABIdClass">
        <key-many-to-one name="a" class="A" column="a_id" />
        <key-many-to-one name="b" class="B" column="b_id" />
    </composite-id>
    <property name="code" type="string">
        <column name="code" not-null="false" default="1.2.3" />
    <property/>
</class>

you can also try something like this: 你也可以尝试这样的事情:

ABIdClass abId = new ABIdClass();
abId.setA(a);
abId.setB(b);
ABClass ab = new ABClass();
ab.setId(abId);
ab.setCode("1.2.3"); 
a.getABClass().add(ab);

hope these be useful. 希望这些有用。

You should be able to achieve what you are looking for using the Hibernate Table per Class Hierarchy approach. 您应该能够使用Hibernate Table per Class Hierarchy方法实现您的目标。

abstract class Mapping { private String code; }
class TableA {}
class TableB {}
class TableAB extends Mapping { private TableA a; private TableB b; }
class TableC {}
class TableD {}
class TableCD extends Mapping { private TableC c; private TableD d; }
class TableE {}
class TableF {}
class TableEF extends Mapping { private TableE e; private TableF f; }

<class name="TableA" table="tableA"/>
<class name="TableB" table="tableB"/>
<class name="TableC" table="tableC"/>
<class name="TableD" table="tableD"/>
<class name="TableE" table="tableE"/>
<class name="TableF" table="tableF"/>
<class name="Mapping" table="mapping">
  <discriminator column="CODE" type="string"/>
  <subclass name="TableAB" discriminator-value="1.2.3">
    <property name="a" column="a_id"/>
    <property name="b" column="b_id"/>
  </subclass>
  <subclass name="TableCD" discriminator-value="4.5.6">
    <property name="c" column="a_id"/>
    <property name="d" column="b_id"/>
  </subclass>
  <subclass name="TableEF" discriminator-value="7.8.9">
    <property name="e" column="a_id"/>
    <property name="f" column="b_id"/>
  </subclass>
</class>

Note that the columns a_id and b_id are common for all relationships. 请注意,列a_idb_id对于所有关系都是通用的。 This will ensure that a single mapping table can be used for different relationships as you want. 这将确保单个映射表可以根据需要用于不同的关系。

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

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