4

目的は、URL を関連付けることです。

次のSQLを使用して、

CREATE TABLE `profiles` (
   `id` serial ,
   `url` varchar(256) ,
    PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `linked_profiles` (
   `profile` bigint unsigned references profiles(id),
   `linked` bigint unsigned references profiles(id),
    PRIMARY KEY  (`profile`, `linked`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

これが休止状態のマッピングです。

<hibernate-mapping>
  <class name="LinkedProfiles" table="linked_profiles">
   <composite-id>
    <key-property name="profile" column="profile" type="long" />
    <key-property name="linked" column="linked" type="long" />
   </composite-id>
   <one-to-one name="profile" class="Profile" cascade="save-update">
   </one-to-one>  
   <one-to-one name="linked" class="Profile" cascade="save-update">
   </one-to-one>       
  </class> 
  <class name="Profile" table="profiles">
     <id name="id" type="long">
       <column name="id" not-null="true"/>
       <generator class="identity"/>
     </id>
     <property name="url" type="java.lang.String">
       <column name="url"/>
     </property>     
  </class>
</hibernate-mapping>

目的: すべての一意の URL には、「プロファイル」テーブルに 1 つのエントリが含まれます。「linked_profiles」は 2 つの URL を関連付けます。

これにより、この例外が発生します。

org.hibernate.MappingException: 次の列マッピングが壊れています: profile.id of: LinkedProfiles

これは Hibernate の欠陥ですか? https://hibernate.atlassian.net/browse/HHH-1771を参照してください。

4

1 に答える 1

0

複合キーとは、コンポーネント キーのいずれかが変更されると、別の一意のキーが作成されることを意味します。これを考慮してください:

 profiles:
id  |url     |
----|--------|
1   |someUrl |
2   |someUrl2|
3   |someUrl3|



linked_profiles:

profile  |linked    |
---------|----------|
1        |3         |->still a valid composite key
1        |2         |->still a valid composite key

これらは、休止状態で追加できるいくつかの例です。したがってone-to-one、例外が発生する Hibernate には意味がありませんorg.hibernate.MappingException: broken column mapping。したがって、変更one-to-oneするmany-to-oneと機能します。

于 2014-07-10T06:14:07.383 に答える