-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Not saving collections
PostPosted: Fri Mar 12, 2004 11:52 am 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
I have an interesting problem from a design point of view using the hibernate object model. We employ a client-server system where the server is Java and the client is C#. The client can make requests that the server run arbitrary HQL queries and return the result. The client has an object model that mimics the hibernate model so the user can change objects and their relationships easily. Then at some point the client will finish it's transaction and any new/deleted/changed objects on the client are persisted back to the server and saved by hibernate.

When objects are sent back to the server they are serialized by the client and rebuilt on the server. I never send collections across so these always end up as null. To prevent my collections getting obliterated when I do session.update() with the object I set inverse=true on the collections so their content is ignored.

This all works fine but now I want a java program to use the same model. This means for unidirectional one-to-many with inverse=true it is impossible to persist collection changes. This means that inverse=true has to go for unidirectional relationships but now I have my collection problem again.

Is there a way I can somehow initialise the collection to a "current" state and then apply any changes made to it by the client. For example I load an object, which has a <set> object inside it, give it to the client which changes one property and then saves it back. All I want to do here is do a session.Update() and not touch the collection at all.

Is there a way to do this using session.load() maybe.

eg

Code:
// some code here receives the changed object from the client....
// including the primary key

myObject = session.load( DBObject.class, primaryKey );

// populate myObject with all the properties of the client object, except the collections

// do update ( is this necessary having done session.load? )
session.Update( myObject );


An example mapping of the sort of object I use.
Hibernate version is 2.1.1 against SQLServer2000

Code:
  <class name="Account" table="account" lazy="true" >

    <id name="AccId" type="int" column="acc_id" unsaved-value="null" >
      <generator class="assigned" />
    </id>
    <version name="VersionId" column="acc_version_id" type="int" />

    <property name="AccHolder" column="acc_holder" type="string" not-null="true" />

    <set name="AccountAddresses" lazy="true" >
      <key column="acc_id"/>
      <one-to-many class="AccountAddress"/>
    </set>
  </class>


For example the client might request an Account object which has 2 AccountAddresses. The AccHolder is changed on the client and the object saved back. In the reconstruction of my object the collection is always left null ( as I don't persist collection proxies, no point really ), which will wipe all the AccountAddresses from the object.

Any help or ideas appreciated.

Cheers.

Myk.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 2:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You could try inverse="true" and set insert="false" , update="false" on the many-to-one.

If that does not work, I suppose you will have to do that manually ... load your old object, and copy the properties over.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.