Hibernate中的自引用实体

时间:2010-04-12 00:52:31

标签: hibernate mapping self-reference

我有一个Action实体,可以在双向一对多关系中将其他Action个对象作为子对象。 问题是Hibernate输出以下异常:

“集合映射中的重复列:DbAction.childs列:actionId”

在映射代码下面:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="DbAction" table="actions">

  <id name="actionId" type="short" />
  <property not-null="true" name="value" type="string" />

  <set name="childs" table="action_action" cascade="all-delete-orphan">
   <key column="actionId" />
   <many-to-many column="actionId" unique="true" class="DbAction" />
  </set>

  <join table="action_action" inverse="true" optional="false">
   <key column="actionId" />
   <many-to-one name="parentAction" column="actionId" not-null="true" class="DbAction" />
  </join>
 </class>
</hibernate-mapping>

2 个答案:

答案 0 :(得分:1)

那是因为您为同一张桌子多次声明了name="actionId"

答案 1 :(得分:1)

正如 armandino 建议的那样,我试图将列名替换为“parentActionId”,并且它可以工作:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="DbAction" table="actions">

  <id name="actionId" type="short" />
  <property not-null="true" name="value" type="string" />

  <set name="childs" table="action_action" cascade="all-delete-orphan">
   <key column="parentActionId" />
   <many-to-many column="actionId" unique="true" class="DbAction" />
  </set>

  <join table="action_action" inverse="true" optional="false">
   <key column="actionId" />
   <many-to-one name="parentAction" column="parentActionId" not-null="true" class="DbAction" />
  </join>
 </class>
</hibernate-mapping>