views:

314

answers:

4

Is there a way to map a factory method in Hibernate (as opposed to having Hibernate call a default constructor and reflectively set properties or fields)?

And if it can't be mapped, does Hibernate provide a hook for custom object creation on a class by class basis?

Thanks!

A: 

See Hibernate and Spring transactions - using private constructors/static factory methods, but not a solution for avoiding the "reflectively set properties or fields" part.

Bert F
+1  A: 

Take a look at UserType. You'd need to call your factory in nullSafeGet() and populate all the fields yourself though. Reverse work is done in nullSafeSet().

Vladimir Dyuzhev
A: 

And if it can't be mapped, does Hibernate provide a hook for custom object creation on a class by class basis?

Check out entity listeners. These add just the annotations that will help you out. Think @PrePersist or @PostLoad.

qbn