4

目标是关联网址。

使用以下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 在“配置文件”表中都有一个条目。'linked_profiles' 关联了两个 url。

这导致了这个异常。

org.hibernate.MappingException:损坏的列映射:profile.id of:LinkedProfiles

这是 Hibernate 的缺陷吗?见https://hibernate.atlassian.net/browse/HHH-1771

4

1 回答 1

0

A composite key means if any of the component key changes it makes another unique key. Now consider this:

 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

These are some examples which hibernate will allow you to add. Hence one-to-one doesn't make sense to Hibernate which results in the Excepetion: org.hibernate.MappingException: broken column mapping. So if you change one-to-one to many-to-one it will work.

于 2014-07-10T06:14:07.383 回答