java - Injecting EntityManager results in NullPointerException in TomEE? -
here's moviebean.java:
@stateless public class moviebean { @persistencecontext private entitymanager em; /** * default constructor. */ public moviebean() { // todo auto-generated constructor stub } public list<movie> getallmovies() { query query = em.createquery("select m movie m"); return query.getresultlist(); }
my moviesservlet
uses moviebean this:
@webservlet("/moviesservlet") public class moviesservlet extends httpservlet { private static final long serialversionuid = 1l; @ejb private moviebean mb; private list<movie> getmovies() { return mb.getallmovies(); }
and in doget
method use getmovies
method list of movies. however, following exceptions:
javax.ejb.ejbexception: bean encountered non-application exception; nested exception is: java.lang.nullpointerexception org.apache.openejb.core.ivm.baseejbproxyhandler.convertexception(baseejbproxyhandler.java:363) org.apache.openejb.core.ivm.baseejbproxyhandler.invoke(baseejbproxyhandler.java:283) bg.cinemate.beans.moviebean$localbeanproxy.getallmovies(bg/cinemate/beans/moviebean.java) bg.cinemate.servlets.moviesservlet.getmovies(moviesservlet.java:27) bg.cinemate.servlets.moviesservlet.doget(moviesservlet.java:42) javax.servlet.http.httpservlet.service(httpservlet.java:621) javax.servlet.http.httpservlet.service(httpservlet.java:728) java.lang.nullpointerexception bg.cinemate.beans.moviebean.getallmovies(moviebean.java:31) sun.reflect.nativemethodaccessorimpl.invoke0(native method) sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) java.lang.reflect.method.invoke(method.java:601) org.apache.openejb.core.interceptor.reflectioninvocationcontext$invocation.invoke(reflectioninvocationcontext.java:181) org.apache.openejb.core.interceptor.reflectioninvocationcontext.proceed(reflectioninvocationcontext.java:163) org.apache.openejb.cdi.cdiinterceptor.invoke(cdiinterceptor.java:126) org.apache.openejb.cdi.cdiinterceptor.access$000(cdiinterceptor.java:42) org.apache.openejb.cdi.cdiinterceptor$1.call(cdiinterceptor.java:63) org.apache.openejb.cdi.cdiinterceptor.aroundinvoke(cdiinterceptor.java:69) sun.reflect.nativemethodaccessorimpl.invoke0(native method) sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) java.lang.reflect.method.invoke(method.java:601) org.apache.openejb.core.interceptor.reflectioninvocationcontext$invocation.invoke(reflectioninvocationcontext.java:181) org.apache.openejb.core.interceptor.reflectioninvocationcontext.proceed(reflectioninvocationcontext.java:163) org.apache.openejb.monitoring.statsinterceptor.record(statsinterceptor.java:176) org.apache.openejb.monitoring.statsinterceptor.invoke(statsinterceptor.java:95) sun.reflect.nativemethodaccessorimpl.invoke0(native method) sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) java.lang.reflect.method.invoke(method.java:601) org.apache.openejb.core.interceptor.reflectioninvocationcontext$invocation.invoke(reflectioninvocationcontext.java:181) org.apache.openejb.core.interceptor.reflectioninvocationcontext.proceed(reflectioninvocationcontext.java:163) org.apache.openejb.core.interceptor.interceptorstack.invoke(interceptorstack.java:138) org.apache.openejb.core.stateless.statelesscontainer._invoke(statelesscontainer.java:239) org.apache.openejb.core.stateless.statelesscontainer.invoke(statelesscontainer.java:191) org.apache.openejb.core.ivm.ejbobjectproxyhandler.synchronizedbusinessmethod(ejbobjectproxyhandler.java:246) org.apache.openejb.core.ivm.ejbobjectproxyhandler.businessmethod(ejbobjectproxyhandler.java:241) org.apache.openejb.core.ivm.ejbobjectproxyhandler._invoke(ejbobjectproxyhandler.java:83) org.apache.openejb.core.ivm.baseejbproxyhandler.invoke(baseejbproxyhandler.java:279) bg.cinemate.beans.moviebean$localbeanproxy.getallmovies(bg/cinemate/beans/moviebean.java) bg.cinemate.servlets.moviesservlet.getmovies(moviesservlet.java:27) bg.cinemate.servlets.moviesservlet.doget(moviesservlet.java:42) javax.servlet.http.httpservlet.service(httpservlet.java:621) javax.servlet.http.httpservlet.service(httpservlet.java:728)
my persistance.xml
file configured this:
<?xml version="1.0" encoding="utf-8" ?> <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" version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"> <persistence-unit name="cinema" transaction-type="resource_local"> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/cinema" /> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.driver" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="password" /> <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> </properties> </persistence-unit> </persistence>
i tried changing (tried 1 shown here) not start server (says time limit has exceeded, after tried changing 200 seconds). use java7, openejb , tomee.
edit 1: changed persistence.xml
this:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> <persistence-unit transaction-type="resource_local" name="cinema"> <jta-data-source>java:openejb/connector/default jdbc database</jta-data-source> <non-jta-data-source>java:openejb/connector/default unmanaged jdbc database</non-jta-data-source> <class>bg.cinemate.beans.moviebean</class> <properties> <property name="openjpa.jdbc.synchronizemappings" value="buildschema(foreignkeys=true)"/> </properties> </persistence-unit> </persistence>
tried @persistencecontext(unitname="cinema")
, server started, gives me same exceptions.
i had same problem while using openjpa2 similar structure. 1 solved nullpointer problem changing transaction-type="resource_local"
transaction-type="jta"
. though in case exception: org.apache.openjpa.persistence.persistenceexception: user lacks privilege or object not found
.
correction: last exception solved , transactions running after mapped correctly datasource tomee persistence.xml
tomee.xml:
<resource id="mydatabaseds" type="datasource"> jdbcdriver org.postgresql.driver jdbcurl jdbc:postgresql://localhost:5432/mydatabase username myuser password mypassword jtamanaged true </resource>
persistence.xml:
<?xml version="1.0" encoding="utf-8"?> <persistence 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" version="2.0"> <persistence-unit name="mypu" transaction-type="jta"> <provider>org.apache.openjpa.persistence.persistenceproviderimpl</provider> <jta-data-source>mydatabaseds</jta-data-source> </persistence-unit> </persistence>
don't forget add jdbc driver lib {tomee}/lib dir.
Comments
Post a Comment