1

I know there are several similar questions on the list, but I've been unsuccessful in twisting them into a solution for my particular problem.

I am using XML mapping files for my hibernate3 configuration. I need to create a group of unidirectional relations between a base object and multiple attachment objects. (The attachments can connect to multiple sites on the base object and each attachment point can hold many attachments. I need to track the lists of attachments separately.) But, doing this I get a "Repeated column in mapping for entity" exception.

org.hibernate.MappingException: Repeated column in mapping for entity: ATTACHMENT column: attachmentID (should be mapped with insert="false" update="false")

The part that is confusing me is that the base object mapping only uses the "attachmentID" as the list index, not the selection key.

Can anyone tell me how to set up these mapping files to avoid the exception? The other similar questions I've found answered on this forum all use annotation, but I need to use the XML.

This is my attachment object. It identifies 3 different kinds of attachment points. One base object type has two possible attachment points and the other has only one. When it attaches to one of the base points it gets the ID of that base object in the appropriate object ID and the rest are set to -1 (which is a special base record).

(Of course, these mappings are radically pared down, but I think I captured all the import features.)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

  <class name="attachment" table="ATTACHMENT">
      <id column="attachmentID" name="attachmentID" type="int">
          <generator class="identity"></generator>
      </id>

      <property name="idBase1"></property>
      <property name="idBase2"></property>
      <property name="idAltBase1"></property>

  </class>
</hibernate-mapping>

This is the mapping file for the base object.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

  <class name="base" table="BASE">
      <id column="IDBASE" name="idBase" type="int">
          <generator class="identity"></generator>
      </id>

    <property name="type"></property>

    <list name="attachment1" cascade="all">
        <key column="idBase1" not-null="true" />
        <list-index column="attachmentID" base="0" />
        <one-to-many class="ATTACHMENT"/>
    </list>

    <list name="attachment2" cascade="all">
        <key column="idBase2" not-null="true"/>    
        <list-index column="attachmentID" base="0" />
        <one-to-many class="ATTACHMENT"/>
    </list>
  </class>
</hibernate-mapping>

and this is the mapping file for the alternate base object. It looks the same as the primary base object but only has one list of attachments. And, I think this one is ok. Or, at least, the exceptions are not being thrown by this mapping.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

  <class name="altBase" table="BASE">
      <id column="IDBASE" name="idBase" type="int">
          <generator class="identity"></generator>
      </id>

    <property name="type"></property>

    <list name="idAltBase1" cascade="all">
        <key column="idBase1" not-null="true" />
        <list-index column="attachmentID" base="0" />
        <one-to-many class="ATTACHMENT"/>
    </list>
  </class>
</hibernate-mapping>
Thom DeCarlo
  • 159
  • 2
  • 12
  • [this](http://stackoverflow.com/a/9442483/507864) might help you understand the **insert="false" update="false"** errors. – ManuPK May 02 '12 at 16:25
  • Is this what you're looking for: http://stackoverflow.com/questions/5338825/how-to-attributeoverride-of-column-names-using-xml-configuration-instead-of-jpa? – Randy Stegbauer May 02 '12 at 19:41
  • I'm still confused. I have a one-to-many, not a many-to-one, and when I added the i=f u=f attributes I got another error that said something like "insert is required in one-to-many relationships" – Thom DeCarlo May 02 '12 at 19:45
  • Sorry, Randy, that I'm such a newb, but I don't see how that helps me. – Thom DeCarlo May 02 '12 at 19:51
  • ManuPK, I was mistaken about the error. When I added the i=f u=f attributes to my one-to-many, Eclipse shows an error: "Attribute "update" must be declared for element type "one-to- many"." And the same thing for the "insert" attribute. – Thom DeCarlo May 02 '12 at 23:18

1 Answers1

1

since attachment maintains the foreign key you have to set inverse on the one-to-many side to tell H that attachment will maintain it

<list name="idAltBase1" cascade="all" inverse="true">

Also when you want to see the foreignkey in attachment, why not map it as references.

  <many-to-one name="base1" column="idBase1"/>
  <many-to-one name="base2" column="idBase2"/>
  <many-to-one name="altBase1" column="idAltBase1"/>

Note: getBase1().getId() will not issue a select for the Base1 row.

Firo
  • 30,626
  • 4
  • 55
  • 94
  • Well, the combination of adding the insert and update attributes to the attachment class and the inverse attribute to the base and altBase classes seems to have worked. Of course, that allowed hibernate to spew even more errors at me, but that's the subject of another thread. Thanks! – Thom DeCarlo May 04 '12 at 13:50