@PersistenceContext with a resource local? either use a jta persistence unit or inject the EntityManagerFactory
*Romain Manni-Bucau* *Twitter: @rmannibucau <https://twitter.com/rmannibucau>* *Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/> *LinkedIn: **http://fr.linkedin.com/in/rmannibucau* *Github: https://github.com/rmannibucau* 2013/2/21 Antonio <[email protected]> > Hi Again, > > Thanks it worked, the name of the bean was not correct but now I'm getting > a > different error: > > > ------------------------------------------------------------------------------- > Test set: com.altobridge.omc.orm.dao.UserDaoLocalTest > > ------------------------------------------------------------------------------- > Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.598 sec > <<< FAILURE! > testCreateUser(com.altobridge.omc.orm.dao.UserDaoLocalTest) Time elapsed: > 4.522 sec <<< ERROR! > javax.ejb.EJBException: The bean encountered a non-application exception; > nested exception is: > java.lang.NullPointerException > at > > org.apache.openejb.core.ivm.BaseEjbProxyHandler.convertException(BaseEjbProxyHandler.java:363) > at > > org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:283) > at $Proxy42.createUser(Unknown Source) > at > > com.altobridge.omc.orm.dao.UserDaoLocalTest.testCreateUser(UserDaoLocalTest.java:47) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at > > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) > at > > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) > at java.lang.reflect.Method.invoke(Method.java:601) > at > > org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) > at > > org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) > at > > org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) > at > > org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) > at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) > at > > org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) > at > > org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) > at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) > at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) > at > org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) > at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) > at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) > at org.junit.runners.ParentRunner.run(ParentRunner.java:300) > at > > org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53) > at > > org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123) > at > > org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at > > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) > at > > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) > at java.lang.reflect.Method.invoke(Method.java:601) > at > > org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) > at > > org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) > at > > org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) > at > > org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) > at > org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68) > Caused by: java.lang.NullPointerException > at com.altobridge.omc.orm.dao.UserDao.createUser(UserDao.java:31) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at > > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) > at > > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) > at java.lang.reflect.Method.invoke(Method.java:601) > at > > org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:181) > at > > org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:163) > at > org.apache.openejb.cdi.CdiInterceptor.invoke(CdiInterceptor.java:128) > at > org.apache.openejb.cdi.CdiInterceptor.access$000(CdiInterceptor.java:43) > at > org.apache.openejb.cdi.CdiInterceptor$1.call(CdiInterceptor.java:64) > at > org.apache.openejb.cdi.CdiInterceptor.aroundInvoke(CdiInterceptor.java:70) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at > > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) > at > > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) > at java.lang.reflect.Method.invoke(Method.java:601) > at > > org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:181) > at > > org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:163) > at > > org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:176) > at > > org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:95) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > at > > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) > at > > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) > at java.lang.reflect.Method.invoke(Method.java:601) > at > > org.apache.openejb.core.interceptor.ReflectionInvocationContext$Invocation.invoke(ReflectionInvocationContext.java:181) > at > > org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:163) > at > > org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:138) > at > > org.apache.openejb.core.stateless.StatelessContainer._invoke(StatelessContainer.java:237) > at > > org.apache.openejb.core.stateless.StatelessContainer.invoke(StatelessContainer.java:189) > at > > org.apache.openejb.core.ivm.EjbObjectProxyHandler.synchronizedBusinessMethod(EjbObjectProxyHandler.java:253) > at > > org.apache.openejb.core.ivm.EjbObjectProxyHandler.businessMethod(EjbObjectProxyHandler.java:248) > at > > org.apache.openejb.core.ivm.EjbObjectProxyHandler._invoke(EjbObjectProxyHandler.java:92) > at > > org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:279) > ... 31 more > > I changed my classes to follow your example of separating business logic > from business model and I modified the classes like that: > > /* > * To change this template, choose Tools | Templates > * and open the template in the editor. > */ > package com.altobridge.omc.orm.dao; > > import com.altobridge.omc.orm.entities.User; > import javax.ejb.Stateless; > import javax.persistence.EntityManager; > import javax.persistence.PersistenceContext; > import javax.persistence.PersistenceContextType; > > /** > * > * @author antonio > */ > @Stateless > public class UserDao implements UserDaoLocal > { > @PersistenceContext > ( > unitName = "com.altobridge_omc-aomdb_ejb_1.0-SNAPSHOTPU", > type = PersistenceContextType.TRANSACTION > ) > private EntityManager entityManager; > > > @Override > public void createUser( User user ) > { > entityManager.persist(user); > } > > @Override > public void deleteUser( User user ) > { > entityManager.remove(user); > } > } > > /* > * To change this template, choose Tools | Templates > * and open the template in the editor. > */ > package com.altobridge.omc.orm.dao; > > import com.altobridge.omc.orm.entities.User; > import javax.ejb.Local; > > /** > * > * @author antonio > */ > @Local > public interface UserDaoLocal > { > public void createUser (User user); > public void deleteUser (User user); > } > > The test is the same > > /* > * To change this template, choose Tools | Templates > * and open the template in the editor. > */ > package com.altobridge.omc.orm.dao; > > import com.altobridge.omc.orm.entities.User; > import java.util.Properties; > import java.util.logging.Level; > import java.util.logging.Logger; > import javax.naming.Context; > import javax.naming.InitialContext; > import javax.naming.NamingException; > import org.junit.Test; > > /** > * > * @author antonio > */ > public class UserDaoLocalTest { > > public UserDaoLocalTest() { > } > > /** > * Test of createUser method, of class UserDaoLocal. > */ > @Test > public void testCreateUser() throws NamingException > { > Properties p = > new Properties(); > p.put(Context.INITIAL_CONTEXT_FACTORY, > "org.apache.openejb.core.LocalInitialContextFactory"); > p.put("openejb.deployments.classpath.ear", "true"); > p.put("postgresDatabase", "new://Resource?type=DataSource"); > p.put("postgresDatabase.JdbcDriver", "org.postgresql.Driver"); > p.put("postgresDatabase.JdbcUrl", > "jdbc:postgresql://localhost:5432/aomdb"); > > Context context = > new InitialContext(p); > UserDaoLocal users = > (UserDaoLocal) context.lookup("UserDaoLocal"); > User user = > new User(); > user.setUsername("Quentin Tarantino"); > user.setId("QuentinTarantino.0"); > users.createUser(user); > } > > /** > * Test of deleteUser method, of class UserDaoLocal. > */ > @Test > public void testDeleteUser() { > } > > > } > > I changed the persistence to look like yours > > <?xml version="1.0" encoding="UTF-8"?> > <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > xsi:schemaLocation="http://java.sun.com/xml/ns/persistence > http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> > <persistence-unit name="com.altobridge_omc-aomdb_ejb_1.0-SNAPSHOTPU" > transaction-type="RESOURCE_LOCAL"> > <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> > <jta-data-source>postgresDatabase</jta-data-source> > ... > <class>com.altobridge.omc.orm.entities.User</class> > ... > <exclude-unlisted-classes>false</exclude-unlisted-classes> > <properties> > <property name="javax.persistence.jdbc.url" > value="jdbc:postgresql://localhost:5432/aomdb"/> > <property name="javax.persistence.jdbc.password" value="omcad1n"/> > <property name="javax.persistence.jdbc.driver" > value="org.postgresql.Driver"/> > <property name="javax.persistence.jdbc.user" value="omcadmin"/> > <property name="openjpa.jdbc.SynchronizeMappings" > value="buildSchema(ForeignKeys=true)"/> > </properties> > </persistence-unit> > </persistence> > > I know that the null pointer is because the entity manager of the bean is > null. > I know is not being injected but i don't what I'm doing wrong with the > context. > > > > -- > View this message in context: > http://openejb.979440.n4.nabble.com/I-can-not-look-up-for-a-bean-in-my-Unit-test-tp4660917p4660932.html > Sent from the OpenEJB User mailing list archive at Nabble.com. >
