簡體   English   中英

休眠集中包含外鍵的組合鍵

[英]Composite key including foreign key in hibernate set

我不明白如何在集合中定義一個復合鍵,包括一個外鍵

這是我的對象:

MyObject                    MySubset
--------                    -------------
String myId                 String subAttribute
String myAttribute          String subValue
Set<MySubset> mySubset

我想要兩個表MyObjectTableMySubsetTable myIdMyObjectTable的主鍵。 我想定義FK 身份識別碼子屬性MySubsetTable的組合鍵。

xml中的休眠映射是什么樣的?

<hibernate-mapping>
  <class table="myObjectTable" name="MyObject">
    <id name="myId">
      <column name="myId"/>
    </id>
    <property name="myAttribute"> <column name=....> </property>
    <set cascade="all, delete-orphan table="MySubsetTable" name"mySubset" ...>

      <!-- How should I define my key? -->


      <composite-element class="MySubset">
        <property name="subAttribute"> <column name="subAttribute"/> </property>
        <property name="subValue"> <column name="subValue"/> </property>
      </composite-element>
    </set>
  </class>
</hibernate-mapping>

我不使用集合而是使用地圖解決了我的問題:

<hibernate-mapping>
  <class table="myObjectTable" name="MyObject">
    <id name="myId">
      <column name="myId"/>
    </id>
    <property name="myAttribute"> <column name=....> </property>
    <map cascade="all, delete-orphan table="MySubsetTable" name"mySubset" ...>
      <key not-null="true" foreign-key="FK_MyObj" column="MyObject"/>
      <map-key type="string" column="myAttribute"/>
      <element type="string" column="myValue"/>
    </map>
  </class>
</hibernate-mapping>

也許這可以幫助別人...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM