Hello,
I am using hibernate to support my domain driven design project. I am currently trying to map my domain model to a normalized database.
The problem:
In my domain model, Address is an immutable value object. I want to be able to freely create them in the domain model. In other words, I don't want my domain model tracking value objects. I don't want the overhead of sharing these value objects. Instead, I want them to be freely created (copied instances of the same value).
NOTE I am referring to Eric Evans DDD Value Objects, not J2EE.
However, because my db is normalized, copies of value objects are causing problems when I commit().
The exception thrown: "a different object with the same identifier value was already associated with this session"
I should add; in the db, ADDRESS is identified by pk(street, buildingNumber, apartmentNumber, postalCode, city) and USER_ADDRESS is the mapping (relationship) table between USER and ADDRESS, where userId identifies USER. Hence, the Address object is identified by all of its attributes using <composite-id> (and yes, I've overriden equal and hashcode).
Current Solution:
For now, the only solution I got is a "find or create" method whenever dealing with shared value objects. I am not to crazy about this, cause it will increase the number of db calls.
Question:
Is there a Hibernate solution to this? is there a way that Hibernate can make value objects sharing in the db level, transparent to the domain ?
_________________ Thanks,
Arturo
|