I'm sorry if these tings have been dealt with before on this forum - I was however not able to find any relevant previous topic, nor any solution by googling.
Basically, I'm having two problems. My application is domain-driven, and thus my entities are part of a rich domain model were I want to restrict access to fields, data and behaviour as much as possible when motivated. One such case is the instantiation of the entities. In many cases, factories are responsible for assembling the objects and enforcing class invariants. This is only a problem in part with Hibernate, thanks to access="field" which is a godsend, since I don't have to expose setters and getters for all my methods. However, I still need to create a default constructor in order for Hibernate to create instances when restoring objects from the database. I'm not pleased with this, even though I may at least give the constructor default-access and disallow illegal instantiation outside of the package. I would like to delegate the restoration/re-assembly of different entities to static factory methods and actual factory instances.
Question: Is this at all possible?
This ties into another problem that I have. I have an aggregate in my domain model that keeps track of certain aspects of the state of things using a couple of value objects that contain far more information than I need, or even feel that I should, persist. I would like to be able to hook into the creation process (@PostLoad?) and reconstruct these value objects using a factory. The same goes, of course for @PrePersist and @PreUpdate, that I need to convert back from these rich value objects to the smaller data structure. It may sound like I just gave the solution to my problem myself, but I do not want to use Annotations.
Questions: Can I define this behaviour in hbm.xml mappings? Must the methods that handle these events be a part of the entity class itself?
I'm thankful for any input and/or discussions that follows...
Regards, Sven
|