views:

418

answers:

2

Hi guys,

I have an Action entity, that can have other Action objects as child in a bidirectional one-to-many relationship. The problem is that Hibernate outputs the following exception:

"Repeated column in mapping for collection: DbAction.childs column: actionId"

Below the code of the mapping:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
<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>
+1  A: 

That's because you have name="actionId" declared more than once for the same table.

armandino
Because I'm referencing the same entity, so the ID field doesn't change.
Marco
Ops, i'm sorry you're right..i thought that it was mandatory to specify the same column names while creating relationships between entities, but it's not. So i resolved the problem by substituting the "actionId" value in the "key" element of the "set" element with "parentActionId", and then setting "parentActionId" in the "column" attribute of the "many-to-one" element.
Marco
A: 

As armandino suggested, i tried to substitute the column name to "parentActionId", and it works:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
<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>
Marco
Glad you got it resolved, Marco.
armandino

related questions