Glassfish JPA:注入 EntityManager 时出现问题

发布于 2024-08-23 13:46:55 字数 2298 浏览 6 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

め七分饶幸 2024-08-30 13:46:55

您会收到 NullPointerException ,因为您正在使用 new() 实例化 BookService - 这基本上是错误的 - 并且没有任何内容被注入到 EJB 中。 EJB 是由容器管理的组件,应该通过注入或查找来获取。

在这里,虽然 JSP 规范允许在 scriplet 中运行任何代码,但实际上并不鼓励从 JSP 调用 EJB,而且 JSP 不支持注入。换句话说,您必须使用查找:

<%@ page import="beans.BookService" %>
<%@ page import="beans.Book" %>
<html>
<body>
<%
    BookService bs = (BookService) new InitialContext().lookup("java:module/BookServiceBean")
    Book b = new Book("Superman");
    bs.createOrUpdate(b);
%>
</body>
</html>

但是您应该从 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 your BookService with a new() - 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:

<%@ page import="beans.BookService" %>
<%@ page import="beans.Book" %>
<html>
<body>
<%
    BookService bs = (BookService) new InitialContext().lookup("java:module/BookServiceBean")
    Book b = new Book("Superman");
    bs.createOrUpdate(b);
%>
</body>
</html>

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).

瞄了个咪的 2024-08-30 13:46:55

尝试:

@PersistenceContext(unitName = "sample")
private EntityManager em;

Try:

@PersistenceContext(unitName = "sample")
private EntityManager em;
最终幸福 2024-08-30 13:46:55

当您确实需要让容器注入它时(通过@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文