I have a domain object that stores a number of attributes as entities retrieved from a lookup table. This has proved tedious and has also coupled my domain model to the physical database schema too closely.
I would simply like to declare a String attribute in the domain object, for example
Code:
String relationship; // rather than Lookup relationship;
and let the mapping retrieve and set the matching (Long) identifier from the lookup table. For example, if I want to store the value "spouse" in my domain object as a string, but the table where the domain object is persisted just has a foreign key id field (long) pointing to the lookup table, can I achieve this with a CompositeUserType?
I was going to use CompositeUserType instead of UserType as it provides the SessionImplementer in the nullSafeGet and nullSafeSet methods so that I can retrieve values from another table with the provided SessionImplementerObject. Can a UserType implementation be used as well?
If this considered a bad practice? I read on p205 of HIA that "..A custom mapping type could perform validation; it culd read and write data to and from an LDAP directory; it could even retrieve persistent objects from a different Hibernate Session for a different database. You're limited mainly by your imagination!"
It just seems that this way allows me to keep my domain objects free of "lookup" objects (or subclasses of lookups) and also easier to persist as I don't have to go to the DAO to get the Lookup object matching the row I want to store in the domain object.
Any advice would be appreciated
Thanks
Alan