Accessing EntityManagerFactory from Seam Interceptor
ktcorby Jun 18, 2008 2:59 PMI am writing an interceptor that needs to access an entitymanagerfactory. How can I accomplish this?
Here is some of my relevant code, in which you can see a couple of the approaches I have tried that don't work.
package com.ccri.gotm.hibernate4gwt; import java.io.Serializable; import javax.naming.InitialContext; import javax.persistence.EntityManagerFactory; import net.sf.hibernate4gwt.core.HibernateBeanManager; import net.sf.hibernate4gwt.core.LazyKiller; import org.jboss.seam.Component; import org.jboss.seam.annotations.intercept.AroundInvoke; import org.jboss.seam.annotations.intercept.Interceptor; import org.jboss.seam.async.AsynchronousInterceptor; import org.jboss.seam.intercept.AbstractInterceptor; import org.jboss.seam.intercept.InvocationContext; @Interceptor(stateless = true, around = AsynchronousInterceptor.class) public class RemoveHibernateTypesInterceptor extends AbstractInterceptor implements Serializable { private InitialContext context; @AroundInvoke public Object aroundInvoke(InvocationContext invocation) throws Exception { context = new InitialContext(); EntityManagerFactory emf = (EntityManagerFactory) context.lookup("java:/entityManagerFactory"); //EntityManagerFactory emf = (EntityManagerFactory) Component.getInstance("entityManagerFactory"); HibernateBeanManager hbm = HibernateBeanManager.getInstance(); hbm.setEntityManagerFactory(emf); LazyKiller lazyKiller = new LazyKiller(null, hbm.getPersistenceUtil()); Object o = invocation.proceed(); o = lazyKiller.detach(o); return o; } }
<?xml version="1.0" encoding="windows-1252"?> <components xmlns="http://jboss.com/products/seam/components" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://jboss.com/products/seam/security" xmlns:core="http://jboss.com/products/seam/core" xmlns:spring="http://jboss.com/products/seam/spring"> <spring:context-loader config-locations="/applicationContext.xml"/> <core:entity-manager-factory name="entityManagerFactory" persistence-unit-name="gotm-ejb" persistence-unit-jndi-name="java:/entityManagerFactory" /> <security:identity authenticate-method="#{authManager.login}"/> </components>