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.