Glassfish JPA:注入 EntityManager 时出现问题
我是 Java EE 新手。我尝试运行一些第一个示例(JPA)。 我正在使用 Glassfish v3。问题是我没有得到应用程序服务器注入 实体管理器。听到的是一个例子 http://www.adam-bien.com/roller/ abien/entry/ejb_3_persistence_jpa_for 我用 JSP 客户端对其进行了扩展。
实体:
package beans;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue
private Long id;
private String title;
private String subtitle;
public Book() {
}
public Book(String title) {
this.title = title;
}
}
BookService 接口:
package beans;
import javax.ejb.Local;
@Local
public interface BookService {
Book createOrUpdate(Book book);
void remove(Book book);
Book find(Object id);
}
BookServiceBean:
package beans;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class BookServiceBean implements BookService {
@PersistenceContext
private EntityManager em;
public Book createOrUpdate(Book book) {
return em.merge(book);
}
public void remove(Book book) {
em.remove(em.merge(book));
}
public Book find(Object id) {
return em.find(Book.class, id);
}
}
persistence.xml:
<persistence>
<persistence-unit name="sample" transaction-type="JTA">
<jta-data-source>jdbc/MarcelsDataSource</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
index.jsp:
<%@ page import="beans.BookServiceBean" %>
<%@ page import="beans.Book" %>
<html>
<body>
<%
BookServiceBean bs = new BookServiceBean();
Book b = new Book("Superman");
bs.createOrUpdate(b);
%>
</body>
</html>
如果我运行该示例,我会在 createOrUpdate() 方法中收到 java.lang.NullPointerException,因此entityManager 是 显然没有正确注入。我已经尝试了好几天寻找补救措施 一些帮助将不胜感激。
谢谢马塞尔
I am new to Java EE. I tried to get some first examples running (JPA).
I am using Glassfish v3. The trouble is that I don't get the App Server injecting
the EntityManager. Hear is one example http://www.adam-bien.com/roller/abien/entry/ejb_3_persistence_jpa_for
which I extended with a JSP client.
Entity:
package beans;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue
private Long id;
private String title;
private String subtitle;
public Book() {
}
public Book(String title) {
this.title = title;
}
}
BookService Interface:
package beans;
import javax.ejb.Local;
@Local
public interface BookService {
Book createOrUpdate(Book book);
void remove(Book book);
Book find(Object id);
}
BookServiceBean:
package beans;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class BookServiceBean implements BookService {
@PersistenceContext
private EntityManager em;
public Book createOrUpdate(Book book) {
return em.merge(book);
}
public void remove(Book book) {
em.remove(em.merge(book));
}
public Book find(Object id) {
return em.find(Book.class, id);
}
}
persistence.xml:
<persistence>
<persistence-unit name="sample" transaction-type="JTA">
<jta-data-source>jdbc/MarcelsDataSource</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
index.jsp:
<%@ page import="beans.BookServiceBean" %>
<%@ page import="beans.Book" %>
<html>
<body>
<%
BookServiceBean bs = new BookServiceBean();
Book b = new Book("Superman");
bs.createOrUpdate(b);
%>
</body>
</html>
If I run the example I get a java.lang.NullPointerException in the createOrUpdate() method so the entityManager is
obviously not injected correctly. I tried to find a remedy for days now and
some help would be highly appreciated.
Thanks
Marcel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您会收到
NullPointerException
,因为您正在使用new()
实例化BookService
- 这基本上是错误的 - 并且没有任何内容被注入到 EJB 中。 EJB 是由容器管理的组件,应该通过注入或查找来获取。在这里,虽然 JSP 规范允许在 scriplet 中运行任何代码,但实际上并不鼓励从 JSP 调用 EJB,而且 JSP 不支持注入。换句话说,您必须使用查找:
但是您应该从 Servlet 或 JSF Managed Bean 调用您的 EJB(并且您的 EJB 可以注入到此类组件中)。
如果您需要一些示例,请查看 Java EE 代码示例和示例。应用程序。
更新:请参阅如何访问本地 EJB EJB FAQ 中的 POJO 组件? 有关 JNDI 的更多详细信息(尤其是新的 EJB 3.1 规范定义的可移植全局 JNDI 名称)。
You get a
NullPointerException
because you are instantiating yourBookService
with anew()
- which is basically wrong - and nothing gets injected in the EJB. EJB are component that are managed by the container and should be obtained either via injection or with a lookup.Here, while the JSP spec allows any code to be run in a scriplet, calling an EJB from a JSP is actually not really encouraged and JSPs don't support injection. In other words, you'll have to use a lookup:
But you should call your EJB from a Servlet or a JSF Managed Bean (and your EJB could be injected in such components).
If you need some samples, have a look at the Java EE Code Samples & Apps.
Update: See How do I access a Local EJB component from a POJO? in the EJB FAQ for more details on JNDI (especially the new portable global JNDI names defined by the EJB 3.1 specification).
尝试:
Try:
当您确实需要让容器注入它时(通过@EJB),您可以直接实例化服务bean。不过,JSP 不支持这一点,因此您还必须切换到 servlet。
You are instantiating the service bean directly, when you really need to be having the container inject it (via @EJB). This isn't supported in a JSP, though, so you'll have to switch to a servlet as well.