In my code I use JSF - Front end, EJB-Middile Tier and JPA connect to DB.Calling EJB using Webservices.Using MySQL as DAtabase. I created a Voter table in which I need to insert a record. I pass values from JSF to EJB, it works. I created a JPA controller class (which automatically generates a save code based on the database classes) Example: getting the object manager, etc.,
em = getEntityManager(); em.getTransaction().begin(); em.persist(voter); em.getTransaction().commit();
I also created a named query:
@NamedQuery(name = "Voter.insertRecord", query = "INSERT INTO Voter v values v.voterID = :voterID,v.password = :password,v.partSSN = :partSSN, v.address = :address, v.zipCode = :zipCode,v.ssn = :ssn, v.vFirstName = :vFirstName,v.vLastName = :vLastName,v.dob = :dob"),
But is it still not possible to insert a record?
Can someone help me insert a record into the database via JPA. (Persistence Object)?
Update:
If we use the container-managed entity manager, we need to write transactions again and again ... for example:
em.getTransaction().begin(); em.getTransaction().commit();
I wrote:
Voter v= new Voter(voterID,password,partSSN,address,zipCode,ssn,vFirstName,vLastName,d1,voterFlag); em.persist(v);
But this leads to a null pointer exception.
SEVERE: java.lang.NullPointerException at ejb.Registration.reg(Registration.java:39) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) at com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseContainer.java:4038) at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5223)