views:

103

answers:

2

Hi all, I"m new to Hibernate. I have 3 tables: Companies, Profiles and Sites. The relation is - one company has many Profiles and Sites (one-to-many).

<hibernate-mapping>

<class name="com.bla.dataobject.CompanyData" table="companies">
 <id name="companyId" column="company_id">
  <generator class="increment"/>
 </id>
 <property name="name" column="company_name" type="java.lang.String"/>
 <property name="description" column="company_information" type="java.lang.String"/>

 <set name="sites" table="company_sites" inverse="true" cascade="all-delete-orphan" lazy="false">
    <key column="company_id" />
    <one-to-many class="com.bla.dataobject.CompanySiteData"/>
 </set>

 <set name="profiles" table="company_profiles" inverse="true" cascade="all-delete-orphan" lazy="false">
    <key column="company_id" />
    <one-to-many class="com.bla.dataobject.CompanyProfile"/>
 </set>
</class>

<class name="com.bla.dataobject.CompanySiteData" table="company_sites">
    <id name="siteId" column="site_id">
        <generator class="increment"/>
    </id>

   <property name="siteProxySettings" column="PROXY_SETTINGS" type="java.lang.String"/>
   .................
   <property name="siteName" column="SITE_NAME" type="java.lang.String"/>


   <many-to-one name="companyData" class="com.bla.dataobject.CompanyData" column="company_id" not-null="true"/>

</class>

<class name="com.bla.dataobject.CompanyProfile" table="company_profiles">
 <id name="profileId" column="profile_id">
  <generator class="increment"/>
 </id>

 <property ............./>

 <many-to-one name="companyData" class="com.bla.dataobject.CompanyData" column="company_id" not-null="true"/>

</class>

The insert and delete works just fine, but not the update. My application has Axis2 servlet on one side and the hibernate on the other. I'm suppling the Company object to the presentation layer via the SOAP, then the presentation layer makes changes to the object and requesting to persist the changes (the returned back object has hibernate id inside). If I just making the update session.update(object); the collection are not updated (but only the Company flat parameters), if I'm getting stored company object from db and perform a merge within 2 objects (like delete all collections and insert a received one and then update a original object) works only if all collection items are new (otherwise I get DB unique constraint on a collection name in the table that already exist). So my questions are: 1. Is it right to try updating "parent object" or it is needed to update all 3 objects separately? 2. What is the right way to update collections (add/remove/update)

I'll really appreciate code example (that I didn't find myself) with collections handling.

Thanks a lot

+1  A: 
  1. try saveorupdate method
  2. set the hibernate.show_sql = true in configuration xml and check the log infomation
Thillakan
A: 

Thank you, Thillakan. I'm already using the seveOrUpdate method, but still has the errors or not saved collections. Maybe the way to do it is - upon receiving new object from presentation layer: 1. fetch the db object by id 2. merge the collections by: a. If the db collection object has same name and equals to received one - do nothing b. if the db collection object has the same name, but not equals to the received one - merge it c. if received object is not in the original collection - add it d. all other objects in the original collection - shall be removed 3. update the changed (fetched in 1.) db object

I just wondering why hibernate can't do it himself (by, for example requiring overriding equals() method by a user)?

Alex