Hi
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