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!
|