-
1. Re: How do I configure a Hibernate Interceptor in Seam?
igx89 Oct 10, 2007 5:02 PM (in response to jakec)I also needed access to the Configuration object (for a different reason), and I worked around it by extending HibernateSessionFactory, overriding its createSessionFactory method, and putting the original code of the method in there along with my modifications:
@Scope(ScopeType.APPLICATION) @BypassInterceptors @Startup public class SmartHibernateSessionFactory extends HibernateSessionFactory { @SuppressWarnings("unchecked") protected SessionFactory createSessionFactory() throws ClassNotFoundException { ... configuration.setInterceptor(...) ... } }
Now, this is definitely a hack: there's code duplication involved, and if a new version of Seam changes anything in createSessionFactory you'll need to remember to carry over the changes. My suggestion to the Seam team would be to add an empty method (maybe called furtherConfiguration) into HibernateSessionFactory and then change the last line in createSessionFactory toreturn furtherConfiguration(configuration.buildSessionFactory());
. That way one could easily change the configuration object without all the maintenance headaches. -
2. Re: How do I configure a Hibernate Interceptor in Seam?
jakec Oct 11, 2007 12:43 PM (in response to jakec)That might work, if I inject currentUser into the Interceptor via @In instead of passing it in through a constructor.
However, I agree that it is a hack. I think I'd rather compare values through reflection and introspection in an @EntityListeners class than get a Hibernate Interceptor in through the back door like that. -
3. Re: How do I configure a Hibernate Interceptor in Seam?
tammy_mc Feb 26, 2013 6:56 PM (in response to igx89)This worked for me. I did have to override and replace the whole HibernateSessionFactory.createSessionFactory() method (which means we HAVE to remember this if we ever update our Seam version).
I added only one single line of code there, right above HibernateSessionFactory.createSessionFactory() call:
configuration.setInterceptor(new HibernateSecurityInterceptor(configuration.getInterceptor()));
I added our own AuditInterceptor so it could be wrapped by HibernateSecurityInterceptor:
configuration.setInterceptor(new AuditInterceptor()); // This is all we added.
Then in components.xml I made changes to use our new AuditableSessionFactory:
<persistence:hibernate-session-factory name="auditableSessionFactory" cfg-resource-name="hibernate.cfg.xml" auto-create="true" />
<persistence:managed-hibernate-session name="hibernateSession" auto-create="true" session-factory="#{auditableSessionFactory}"/>
The 2 classes I wrote are AuditInterceptor and AuditableSessionFactory
AuditableSessionFactory extends HibernateSessionFactory and just overrides the one method createSessionFactory() replacing the single line as I describe above.
AuditableSessionFactory has to have the following Seam annotations declared on it as well:
@Scope(ScopeType.APPLICATION)
@Install(precedence=Install.DEPLOYMENT)
@BypassInterceptors
@Name("auditableSessionFactory")
@Startup
public class AuditableSessionFactory extends HibernateSessionFactory
The @Install annotation increases the precedence of my AuditableSessionFactory so Seam chooses to use it over the HibernateSessionFactory which is also annotated to be created at app startup.
And AuditInterceptor extends EmptyInterceptor and then just overrides the methods I am interested in for updating some properties onFlushDirty (update) and onSave (new).
-
4. Re: How do I configure a Hibernate Interceptor in Seam?
ibenjes Apr 2, 2013 9:13 AM (in response to tammy_mc)Just enabling a Hibernate Interceptor (per Session Factory) in Seam is easy, you don't have to overwrite the HibernateSessionFactory.
In the persistence.xml file you just add
<property name="hibernate.ejb.interceptor" value="com.yourcompany.hibernate.HibernateInterceptor" />
The problem is how do you enable a 'per Session' Hibernate Interceptor in Seam?