这是我的 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 FROM Movie m");
return query.getResultList();
}
我MoviesServlet
像这样使用这个 MovieBean:
@WebServlet("/MoviesServlet")
public class MoviesServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@EJB
private MovieBean mb;
private List<Movie> getMovies() {
return mb.getAllMovies();
}
在我doGet
的方法中,我使用该getMovies
方法来获取所有电影的列表。但是,我得到以下例外:
javax.ejb.EJBException: The bean encountered a 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)
我的persistance.xml
文件配置如下:
<?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>
我尝试更改它(尝试使用此处显示的那个)但它甚至没有启动服务器(说时间限制已超过,即使我尝试将其更改为 200 秒)。我使用 Java7、OpenEJB 和 TomEE。
编辑 1:
我将我的更改persistence.xml
为:
<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>
尝试@PersistenceContext(unitName="cinema")
并启动了服务器,但它给了我同样的例外。