tag:blogger.com,1999:blog-2424622516012859853.post6454743528292314398..comments2023-05-01T06:47:05.584-07:00Comments on Fresh Espresso: Hibernate Interceptors, Events and JPA Entity Listenerserthttp://www.blogger.com/profile/07418191492358888029noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-2424622516012859853.post-11679038244247998522010-04-02T07:36:53.177-07:002010-04-02T07:36:53.177-07:00interesting summary, thanks!
i've found two o...interesting summary, thanks!<br /><br />i've found two other options to configure hibernate Event Listeners in JPA environment <br /><br />1) either directly in presistence.xml in the properties element the same way you configure the Interceptor, with property names "hibernate.ejb.event.post-update" and so on.<br /><br />this way hibernate instantiates the listeners on startup.<br /><br />2) or provide your own PersistenceProvider - subclass org.hibernate.ejb.HibernatePersistence and inject the EventListeners directly to the Ejb3Configuration before the entity manager factory is built. <br /><br />this way you (or e.g. spring) can manage the EventListener instances. however this is very tightly coupled to the hibernate configuration implementation i'm using(3.4.0.GA) and it may happen that your event listeners won't be properly initialized (e.g. CallbackHandlerConsumer<br /> and JACCSecurityListener implementations, check org.hibernate.ejb.EventListenerConfigurator.configure()).Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2424622516012859853.post-65011130482459484862009-06-03T05:28:34.705-07:002009-06-03T05:28:34.705-07:00Regarding Hibernate Interceptors, i have posted th...Regarding Hibernate Interceptors, i have posted the following <a href="https://forum.hibernate.org/viewtopic.php?f=1&t=997310" rel="nofollow">hibernate post</a>.<br /><br /><br />what do you think ?naakahttps://www.blogger.com/profile/14690042906963467775noreply@blogger.comtag:blogger.com,1999:blog-2424622516012859853.post-33014218200496723892009-06-03T05:07:21.547-07:002009-06-03T05:07:21.547-07:00I have modified the session impl to generate the i...I have modified the session impl to generate the id also for objects instanciated from any interceptor:<br /><br />public Object instantiate(EntityPersister persister, Serializable id) throws HibernateException {<br /> errorIfClosed();<br /> checkTransactionSynchStatus();<br /> Object result = interceptor.instantiate( persister.getEntityName(), entityMode, id );<br /> if ( result == null ) {<br /> result = persister.instantiate( id, entityMode );<br /> } else {<br /> persister.setIdentifier(result, id, entityMode);<br /> }<br /> return result;<br />}<br /><br />for my use case seems to work pretty well.<br /><br />Is there a reason why user should control also the setting of the id through an interceptor ?<br /><br />To me no....naakahttps://www.blogger.com/profile/14690042906963467775noreply@blogger.comtag:blogger.com,1999:blog-2424622516012859853.post-68780534560663156192009-03-27T01:38:00.000-07:002009-03-27T01:38:00.000-07:00For JPA Entity Listeners, under OpenJPA, you can g...<A HREF="http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html" REL="nofollow">For JPA Entity Listeners, under OpenJPA, you can get the previous entity state</A>.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2424622516012859853.post-22090638716472469762008-11-17T04:55:00.000-08:002008-11-17T04:55:00.000-08:00I think as long as you make sure you open a new se...I think as long as you make sure you open a new session you should be ok. You're just need to very careful not to intercept that new transaction, so you may want to consider dropping a message on an event queue and then writing a service that listens to this event queue to do your logging.erthttps://www.blogger.com/profile/07418191492358888029noreply@blogger.comtag:blogger.com,1999:blog-2424622516012859853.post-73967987787053519532008-11-16T20:11:00.000-08:002008-11-16T20:11:00.000-08:00Kurt,So then would you say logging object metadata...Kurt,<BR/><BR/>So then would you say logging object metadata (time, user) and writing this data to the database would be a *bad* example of proper usage of these interceptors? I'd like to build a "global" logging function that would extend to all entities without needing to define this logic for each entity. I've been looking around for solutions and I'm thinking of using either @PostPersist or interceptors. Which would you suggest?<BR/><BR/>Thanks for the useful information.Chrishttps://www.blogger.com/profile/08551205530912021890noreply@blogger.comtag:blogger.com,1999:blog-2424622516012859853.post-53248633532586875992008-10-20T21:19:00.000-07:002008-10-20T21:19:00.000-07:00As far as I understand it you're not supposed to d...As far as I understand it you're not supposed to do any db calls in the interceptor. I ended up only capturing the entity itself, and the IDs of the related objects, as entity.getChildObject.getId() does not need to load up the ChildObject. If you do attempt to load up more objects, you may run into "no session" exceptions, if the object was loaded in a transaction that is no longer open. Which in turn will mark the current transaction for rollback.<BR/><BR/>This obviously limits the power of interceptor pretty severly. If you object is HibernateProxy you can get to some extra information using:<BR/><BR/>if (object instanceof HibernateProxy) {<BR/> HibernateProxy proxy = (HibernateProxy) object;<BR/> implementationClass = proxy.getHibernateLazyInitializer().getImplementation().getClass();<BR/> }<BR/><BR/>Well I'm interested to hear what you find out.<BR/><BR/>Hope this helps you.<BR/><BR/>--Kurterthttps://www.blogger.com/profile/07418191492358888029noreply@blogger.comtag:blogger.com,1999:blog-2424622516012859853.post-18118562961684101202008-10-20T12:49:00.000-07:002008-10-20T12:49:00.000-07:00How do we get a handle to the EM/DAO inside of the...How do we get a handle to the EM/DAO inside of the interceptor callback when the interceptor class is defined in the persistence.xml?<BR/><BR/>thanks.Anonymousnoreply@blogger.com