Hibernate – uses producers in Tomcat to inject EntityManager

I am running a project using Hibernate and Weld CDI on tomcat 7. I have written a ServletContextListener to create EntityManagerFactory and EntityManager during application startup.

< p>

public class PersistenceListener implements ServletContextListener {

private static EntityManagerFactory entityManagerFactory;

public void contextInitialized(ServletContextEvent sce){
ServletContext context = sce.getServletContext();
entityManagerFactory = Persistence.createEntityManagerFactory("hibernate-test");
}

public void contextDestroyed(ServletContextEvent sce) {
entityManagerFactory .close();
}


public static EntityManager createEntityManager() {
if (entityManagerFactory == null) {
throw new IllegalStateException(" Context is not initialized yet.");
}

return entityManagerFactory.createEntityManager();
}
< br />)

I can create it with the following code and use my entityManager in my test class (it is an arquillian test class)

EntityManager em = PersistenceListener.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from Game").executeUpdate();
em.getTransaction ().commit();

This is the complete code of my test class

@RunWith(Arquillian.class)
public class HibernateTestSample {

@Deployment
public static WebArchive createTestArchive()
{
MavenDependencyResolver resolver = DependencyResolvers.use(
MavenDependencyResolver.class).loadMetadataFromPom(" pom.xml");

WebArchive webArchive= ShrinkWrap
.create(WebArchive.class, "ROOT.war")
.addClasses(CdiTestBean.class,HibernateListener.class, PersistenceListener.class)
.addAsLibrari es(
resolver.artifact("org.jboss.weld.servlet:weld-servlet")
// .artifact("org.hibernate.javax.persistence:hibernate-jpa-2.0-api" )
.artifact("org.apache.tomcat:tomcat-dbcp")
.artifact("org.hibernate:hibernate-entitymanager")
.artifact("org.hibernate:hibernate -validator")
.artifact("org.hibernate:hibernate-core")
.artifact("com.h2database:h2")
.artifact("mysql:mysql-connector- java")
.resolveAs(GenericArchive.class))

.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource("test-persistence.xml" , "classes/META-INF/persistence.xml")
.addAsWebInfResource("hibernate .cfg.xml", "classes/hibernate.cfg.xml")
// .addAsWebInfResource("context.xml", "classes/META-INF/context.xml")
.addAsManifestResource( "context.xml", "context.xml")
.setWebXML("hibernate-web.xml");
System.out.println(webArchive.toString(true));

return webArchive;
}


@Test
public void myTest()
throws Exception {

EntityManager em = PersistenceListener.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from Game").executeUpdate();
em.getTransaction( ).commit();
...............
.......
...


}
}

But I want to change my e ntityManager injects my class. I read in an other post, I can’t use @PersistenceContext in my class, so I decided to use producer to inject my entity manager. But it doesn’t work for me, please let me know What am I doing wrong here (I am new to CDI)

This is my new ServletContextListener

public class PersistenceListener implements ServletContextListener {

private static EntityManagerFactory entityManagerFactory;

@Produces
private EntityManager entityManager;

public void contextInitialized(ServletContextEvent sce){
ServletContext context = sce.getServletContext();
entityManagerFactory = Persistence.createEntityManagerFactory("hibernate-test");
createEntityManager();
}

public void contextDestroyed(ServletContextEvent sce ) {
entityManagerFactory.close();
}


public void createEntityManager() {
if (entityManagerFactory == null) {
throw new IllegalStateException("Context is not initialized yet.");
}

this.entityManager = entityManagerFactory.createEntityManager();
}

And I am injecting in my test class

@Inject
private EntityManager em;

It is null

You need to use @Produces on the createEntityManager method, not fields.

I run a project using Hibernate and Weld CDI on tomcat 7. I have written a ServletContextListener to Create EntityManagerFactory and EntityManager during application startup.

public class PersistenceListener implements ServletContextListener {

private static EntityManagerFactory entityManagerFactory;

public void contextInitialized(ServletContextEvent sce){
ServletContext context = sce.getServletContext();
entityManagerFactory = Persistence.createEntityManagerFactory("hibernate-test");
}

public void contextDestroyed(ServletContextEvent sce) {
entityManagerFactory.close();
}


publ ic static EntityManager createEntityManager() {
if (entityManagerFactory == null) {
throw new IllegalStateException("Context is not initialized yet.");
}

return entityManagerFactory.createEntityManager();
}

}

I can create it by the following code, in my test class (it is an arquillian test class) Use my entityManager

EntityManager em = PersistenceListener.createEntityManager();
em.getTransaction().begin();
em.createQuery(" delete from Game").executeUpdate();
em.getTransaction().commit();

This is the complete code of my test class

@RunWith(Arquillian.class)
public class HibernateTestSample {

@Deployment
public static WebArchive createTestArchive()
{
MavenDependencyResolver resolver = DependencyResolvers.use(
MavenDependencyResolver.class).lo adMetadataFromPom("pom.xml");

WebArchive webArchive= ShrinkWrap
.create(WebArchive.class, "ROOT.war")
.addClasses(CdiTestBean.class,HibernateListener .class,PersistenceListener.class)
.addAsLibraries(
resolver.artifact("org.jboss.weld.servlet:weld-servlet")
// .artifact("org.hibernate. javax.persistence:hibernate-jpa-2.0-api")
.artifact("org.apache.tomcat:tomcat-dbcp")
.artifact("org.hibernate:hibernate-entitymanager")< br /> .artifact("org.hibernate:hibernate-validator")
.artifact("org.hibernate:hibernate-core")
.artifact("com.h2database:h2")
.artifact("mysql:mysql-connector-java")
.resolveAs(GenericArchive.class))

.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsWebInfResource("test-persistence.xml", "classes/META-INF /persistence.xml")
.addAsWebInfResource("hibernate.cfg.xml", "classes/hibernate.cfg.xml")
// .addAsWebInfResource("context.xml", "classes/META -INF/context.xml")
.addAsManifestResource("context.xml", "context.xml")
.setWebXML("hibernate-web.xml");
System.out .println(webArchive.toString(true));

return webArchive;
}


@Test
public void myTest()
throws Exception {

EntityManager em = PersistenceListener.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from Game").executeUpdate();
em.getTransaction().commit();
............. ..
.......
...


}
}

But I want to The entityManager is injected into my class. I read in an other post, I cannot use @PersistenceContext in my class, so I decided to use the producer to inject my entity manager. But it does not work for me, please tell What am I doing wrong here (I am new to CDI)

This is my new ServletContextListener

public class PersistenceListener implements ServletContextListener {

private static EntityManagerFactory entityManagerFactory;

@Produces
private EntityManager entityManager;

public void contextInitialized(ServletContextEvent sce){
ServletContext context = sce.getServletContext();
entityManagerFactory = Persistence.createEntityManagerFactory("hibernate-test");
createEntityManager();
}

public void contextDestroye d(ServletContextEvent sce) {
entityManagerFactory.close();
}


public void createEntityManager() {
if (entityManagerFactory == null) {
throw new IllegalStateException("Context is not initialized yet.");
}

this.entityManager = entityManagerFactory.createEntityManager();
}

And I am injecting into my test class

@Inject
private EntityManager em;

It is null

You need to use @Produces on the createEntityManager method, not fields.

Leave a Comment

Your email address will not be published.