Hibernate saveOrUpdate Objects with Lists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Humakt
    New Member
    • Aug 2008
    • 42

    Hibernate saveOrUpdate Objects with Lists

    I'm wondering how to do saveOrUpdate properly for Objects that contain lists of other objects. The way it now does it for me, is that saveOrUpdate works fine with object itself, but it adds new entries for the object's list even though those entries already exist. In other words, it is duplicating data unnecessary.

    Could someone help or show me how such a mapping is done? I have the hibernate mapping files, but do not have access to them from this computer. I can post them later if they are deemed necessary.

    I'm using "assigned" for parent object's generator-class.
  • Humakt
    New Member
    • Aug 2008
    • 42

    #2
    Help anyone?

    Here are sample of mapping files.

    The main object:

    Code:
    <?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">
    <!-- Generated 13.11.2009 9:13:54 by Hibernate Tools 3.2.1.GA -->
    <hibernate-mapping>
      <class catalog="ohtu_s09_r2" name="lukki.pavut.Toteutus" table="Toteutus">
        <id name="totid" type="string">
          <column name="totid"/>
          <generator class="assigned"/>
        </id>
        <property name="alkuper" type="date">
          <column name="alkuper"/>
        </property>
        <property name="loppuper" type="date">
          <column name="loppuper"/>
        </property>
        <property name="kurssi" type="string"/>
        <list cascade="all" name="opettajat">
          <key column="totid" not-null="true"/>
          <index column="idx"/>
          <one-to-many class="lukki.pavut.Henkilot"/>
        </list>
        <list cascade="all" name="ryhmat">
          <key column="totid"/>
          <index column="idx"/>
          <one-to-many class="lukki.pavut.Ryhmat"/>
        </list>
        <list cascade="all" name="totajat">
          <key column="totid"/>
          <index column="idx"/>
          <one-to-many class="lukki.pavut.Toteutuksenajat"/>
        </list>
        <list cascade="all" name="kevytkotisivut">
          <key column="totid"/>
          <index column="idx"/>
          <one-to-many class="lukki.pavut.KevytKotiSivu"/>
        </list>
        <list cascade="all" name="suunnitelmat">
          <key column="totid"/>
          <index column="idx"/>
          <one-to-many class="lukki.pavut.Suunnitelma"/>
        </list>
      </class>
    </hibernate-mapping>
    One of the mappings for list object:

    Code:
    <?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">
    <!-- Generated 13.11.2009 9:13:54 by Hibernate Tools 3.2.1.GA -->
    <hibernate-mapping>
      <class catalog="ohtu_s09_r2" name="lukki.pavut.Toteutuksenajat" table="Toteutuksenajat">
        <id name="id" type="java.lang.Integer">
          <column name="id"/>
          <generator class="identity"/>
        </id>
        <property name="alkua" type="java.lang.Integer">
          <column name="alkua"/>
        </property>
        <property name="kesto" type="java.lang.Integer">
          <column name="kesto"/>
        </property>
        <property name="luokka" type="string">
          <column length="65535" name="luokka"/>
        </property>
        <property name="paiva" type="java.lang.Integer">
          <column name="paiva"/>
        </property>
      </class>
    </hibernate-mapping>

    Comment

    Working...