Hi guys,
can someone with JPA experience PLEASE help me. All I want to do is persist
an entity to a database. Sounds easy yes? I have been trying for two
weeks solid now to persist a simple Persons class to a database with a table
called persons with an id, firstName and age field. I have asked in many
java forums but come up with the same answers, which don't help. I am
trying to use a container injected EJB for the work. I am using netbeans
(comes with glassfish 3.1.1). I can read from the database no problem but I
keep getting a null pointer exception when I try to persist to it. I have a
simple jsp page with a form that takes the users first name and age, when
they hit submit I want to store it in a database.
My servlets POST method which takes the users info and send the object to be
persisted:
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
String userPath = request.getServletPath();
if(userPath.equals("/result")){
String firstName = request.getParameter("firstName");
String age = request.getParameter("age");
Persons p = new Persons();
p.setFirstName(firstName);
p.setAge(age);
pf.create(p);
}
// use RequestDispatcher to forward request internally
String url = "/WEB-INF/view" + userPath + ".jsp";
try {
request.getRequestDispatcher(url).forward(request,
response);
} catch (Exception ex) {
ex.printStackTrace();
}
}
The PersonFacade class generated by netbeans "create session from entiy
classes" wizard and its associated abstract facace:
@Stateless
public class PersonsFacade extends AbstractFacade<Persons> {
@PersistenceContext(unitName = "FormTestPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public PersonsFacade() {
super(Persons.class);
}
}
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq =
getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq =
getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q =
getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0]);
q.setFirstResult(range[0]);
return q.getResultList();
}
public int count() {
javax.persistence.criteria.CriteriaQuery cq =
getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q =
getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
}
The entity class:
@Entity
@Table(name = "persons")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Persons.findAll", query = "SELECT p FROM Persons
p"),
@NamedQuery(name = "Persons.findById", query = "SELECT p FROM Persons
p WHERE p.id = :id"),
@NamedQuery(name = "Persons.findByAge", query = "SELECT p FROM Persons
p WHERE p.age = :age"),
@NamedQuery(name = "Persons.findByFirstName", query = "SELECT p FROM
Persons p WHERE p.firstName = :firstName")})
public class Persons implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "Id")
private Integer id;
@Size(max = 255)
@Column(name = "Age")
private String age;
@Size(max = 255)
@Column(name = "FirstName")
private String firstName;
public Persons() {
}
public Persons(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
My persistence.xml file:
<persistence version="2.0" xmlns="
http://java.sun.com/xml/ns/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">
<persistence-unit name="FormTestPU" transaction-type="JTA">
<jta-data-source>jdbc/formtest</jta-data-source>
<class>entity.Persons</class>
<properties/>
</persistence-unit>
</persistence>
I know that the entity manager in the person facade class is not meant to be
instantiated because it is injected by the container so I am at a loss to why
I am getting a null pointer exception.
Any help would be greatly appreciated!
Thanks,
Alan
--
[Message sent by forum member 'asmith2306']
View Post: http://forums.java.net/node/883335