Glassfish JPA: Problems injecting 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
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).
Try:
@PersistenceContext(unitName = "sample")
private EntityManager em;
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.
精彩评论