-
1. Re: Component.getInstance insided a @PrePersist method
Drew Kutchar Oct 21, 2008 5:37 AM (in response to Jason Long)I don't like this approach since it's adding Seam dependency to your model. Unnecessary complexity.
Plus the code for setting the created and creator can be refactored out to an Interceptor, DAO layer, EntityHome (if your using Seam Framework) or even an event listener and applied to all the object who implement an interface, say CreatorAware or something.
my $.02
-
2. Re: Component.getInstance insided a @PrePersist method
Jason Long Oct 21, 2008 5:44 AM (in response to Jason Long)I agree and had considered that approach, but did not know which would be best.
The event listener sounds good. Can you point me to an example or give some more tips?
-
3. Re: Component.getInstance insided a @PrePersist method
Stuart Douglas Oct 21, 2008 6:12 AM (in response to Jason Long)You probably want to use an ejb3 entity listener to listen for the postLoad event, something like:
orm.xml:
<persistence-unit-metadata> <persistence-unit-defaults> <entity-listeners> <entity-listener class="com.example.EntityLister"> <pre-persist method-name="prePersist"/> </entity-listener> </entity-listeners> </persistence-unit-defaults> </persistence-unit-metadata>
Have an interface as follows:
interface CreatorAware { public void setCreator(User u); public User getCreator(); }
and then in the entity lister:
class EntityLister { public void prePersist(Object entity) { if(CreatorAware.class.isAssignableFrom(entity.getClass()) { CreateAware a = (CreatorAware)entity; if(a.getCreator() == null) { a.setCreator((Usr) Component.getInstance("currentUsr", ScopeType.SESSION)); } } } }
-
4. Re: Component.getInstance insided a @PrePersist method
Drew Kutchar Oct 21, 2008 6:19 AM (in response to Jason Long)Take a look at
JPA-Hibernate Event Listener: http://tinyurl.com/5k764b
Hibernate Event Listener: http://tinyurl.com/5dj5uu
Seam Interceptors: http://tinyurl.com/6c9lqb