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.  [ 3 posts ] 
Author Message
 Post subject: Interesting design problem
PostPosted: Fri Sep 08, 2006 10:46 am 
Beginner
Beginner

Joined: Tue Oct 19, 2004 11:04 am
Posts: 22
Hibernate version: 3.1

I have an interesting design problem about which I would like some input from you (the informed masses).

I have a class that has a single property that I don't want updated each time the rest of the class is updated (I will explain further below). For starters (and simplicity):

class MyClass {
String id;
String a;
String b;
int version;
}

Let's say the property in question is 'b'. When I use Hibernate to select the class from the database, I want full access to property 'a', read-only access to 'b'. When I update an instance of MyClass, I only want to update 'a'. In other words, I need to (optimistically) version 'b' separately from 'a'.

In order to accomplish this, my first thoughts were to break out 'b' into its own component or entity. Something like this:

class MyClass {
String id;
String a;
MyClassB b;
int version;
}

class MyClassB {
String id;
String b:
int version;
}

There now is a one-to-one unidirectional relationship between MyClass and MyClassB. Whether or not MyClassB is its own entity (has its own identifier) or is a composite element of MyClass is still up in the air.

My goal now is to have this scenario:

* When retrieving MyClass instances from the database, the MyClassB instance that belongs to it should also be pulled.
* When deleting MyClass instances, the associated MyClassB instance should also be automagically deleted by Hibernate.
* When updating MyClass instances, the associated MyClassB instance SHOULD NOT be updated.
* MyClassB instances should be updatable individually.

(The reason for all of this is based on concurrency and my use cases and explaining that in detail is not relevant for the questions I have. Please keep in mind that I have simplified things for the example.)

Here are my questions:

Overall, what is the best strategy, taking into account the hibernate configuration, for setting this up?

Should MyClassB be its own entity or should it be a composite element of MyClass? Can I accomplish my requirements using a composite?

What cascade options should I use?

What else, configuration or code wise, do I need to take into account to satisfy my requirements?

There seems to be a lot of options and if anyone has experience with something similar, your input would be greatly appreciated.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 08, 2006 2:20 pm 
Senior
Senior

Joined: Sun Jun 04, 2006 1:58 am
Posts: 136
from HIA

By default, Hibernate does not navigate an association when searching for transient or detached objects, so saving, deleting, or reattaching a Category won?t affect the child objects. If, for a particular association, you wish to enable transitive persistence, you must override this default in the mapping metadata.
You can map entity associations in metadata with the following attributes:
1) cascade="none", the default, tells Hibernate to ignore the association.
2) cascade="save-update" tells Hibernate to navigate the association when the transaction is committed and when an object is passed to save() or update() and save newly instantiated transient instances and persist changes to detached instances.
3) cascade="delete" tells Hibernate to navigate the association and delete persistent instances when an object is passed to delete().
4) cascade="all" means to cascade both save-update and delete, as well as calls to evict and lock.
5) cascade="all-delete-orphan" means the same as cascade="all" but, in addition, Hibernate deletes any persistent entity instance that has been removed (dereferenced) from the association (for example, from a collection).

6) cascade="delete-orphan" Hibernate will delete any persistent entity instance that has been removed (dereferenced) from the association (for example, from a collection).

_________________
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 08, 2006 7:33 pm 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
easiest way is to set update="false" when mapping property b.
-----------------
don't forget credits

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.