Hibernate version:
2.17
I've read the docs (e.g.
http://www.hibernate.org/hib_docs/reference/en/html/), I've read the FAQs, I've read
Hibernate In Action, I've looked through the forums, but I still haven't found a good answer to my question. Or, more likely, I'm just unwilling to accept the answer I've been given and just want to make sure there is not a better way. I would appreciate any help on any of the questions I raise below. If you want to tell me to RTFM, please give me an URL to go to.
I'm trying to create a
smart Domain Model. I've got a business object of Customer. Customer has Name, Address, PhoneNumber. all 3 of which are values, not entities. All 3 also have validation semantics, e.g. you cannot create a PhoneNumber from a String that only has 4 digits.
Now I have some choices. Should PhoneNumber be immutable? I think so, since there are no operations on it in this application other than set, read, and query. In fact, it is merely a String with a validation rule and canonicalization rule wrapped around it. Name and Address are similar, except they are composed of other smart domain objects, like Street and ZipCode. Nearly (but not entirely) all the domain objects are Strings at the finest grain.
Another good thing about them being immutable is that I don't have to worry about having an invalid ZipCode object. If I have a ZipCode object, it represents a valid ZipCode. The only way to set a new ZipCode is with another ZipCode or by creating one with a String, but if the String doesn't represent a valid ZipCode, the create fails and you are left with null. Otherwise, I have to create an internal state about whether or not this ZipCode is valid and check it everywhere I use it.
The thing is, with
Hibernate, even though I'm using these only as values, if I make them immutable, then as far as I can tell I have to create UserTypes for them, and the UserTypes are more complicated than the types themselves. I can't just map them as component properties, even with access=field, because they don't have default constructors. (If they did have default constructors, I'd be back to having to worry about the objects being invalid.)
Hibernate In Action (p. 40) tells me I should use the Validatable interface, either.
* Is it true that the
best practice is for me to write UserTypes for every one of these types (Name, Address, ZipCode, PhoneNumber)?
Isn't their an easier way? If there is, please tell me about it.
If I really do have to create all thouse UserTypes:
* What should I do with nulls in nullSafeSet/Get()? Is it OK to return null from nullSafeGet()? Is it OK to throw an exception from nullSafeSet()? If so, what exception should I throw? Remember, I want to avoid creating an object with an invalid state.
* Does that mean that Address is a CompositeUserType (since it includes a ZipCode)?
* Why does the JavaDoc for CompositUserType.disassemble() say that it should perform a deep copy? If the object to be cached is Serializable then why can't I just return it?
Thank you in advance for your help.