i am working with hibernate 3.3.1,jdk1.6.0, jboss5.0.0GA.
I have written persistence .xml in
%CONTEXT_ROOT%\WEB-INF\classes\META-INF like
<persistence 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/ ... ce_1_0.xsd"
version="1.0">
<persistence-unit name="simple" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/XAOracleDS</jta-data-source>
<properties>
<property name="hibernate.archive.autodetection"
value="class,hbm" />
<property name="hibernate.show_sql"
value="true" />
<property name="hibernate.format_sql"
value="true" />
<property name="hibernate.c3p0.min_size"
value="2" />
<property name="hibernate.c3p0.max_size"
value="5" />
<property name="hibernate.c3p0.timeout"
value="300" />
<property name="hibernate.c3p0.max_statements"
value="15" />
<property name="hibernate.c3p0.idle_test_period"
value="300" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.Oracle9Dialect" />
</properties>
</persistence-unit>
</persistence>
then wrote a servlet like
package myjpas;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;
import javax.annotation.Resource;
import javax.persistence.PersistenceContextType;
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;
@PersistenceContext(name="java:/myJPA",
unitName="simple")
public class JPASafeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Resource(name="java:/myJPA")
EntityManager em;
public void init(){
System.out.println("bbbbbb");
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
String id = request.getParameter("userId");
long userId = 0L;
int age = 0;
if(id!=null || !(id.isEmpty())) {
userId = Long.parseLong(id);
}
String uname = request.getParameter("username");
String email = request.getParameter("email");
String ageStr = request.getParameter("age");
if(ageStr!=null || !(ageStr.isEmpty())) {
age = Integer.parseInt(ageStr);
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
EntityTransaction trans = em.getTransaction();
UserTO user = new UserTO(userId,uname,email,age);
if(em !=null) {
System.out.println("..."+em.isOpen());
trans.begin();
em.persist(user);
trans.commit();
out.println("User saved");
}
else {
out.println("User not saved");
}
em.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
but when i am running , iam getting Nullpointer exception on server console
(HDScanner) Fully Deployed vfszip:/C:/jboss500/server/standard/deploy/jpaweb.war
2009-04-11 11:43:17,442 INFO [STDOUT] (http-127.0.0.1-10080-1) bbbbbb
2009-04-11 11:43:17,455 ERROR [STDERR] (http-127.0.0.1-10080-1)
java.lang.NullPointerException
2009-04-11 11:43:17,455 ERROR [STDERR] (http-127.0.0.1-10080-1) at
myjpas.JPASafeServlet.doPost(Unknown Source)
2009-04-11 11:43:17,455 ERROR [STDERR] (http-127.0.0.1-10080-1) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
2009-04-11 11:43:17,456 ERROR [STDERR] (http-127.0.0.1-10080-1) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
2009-04-11 11:43:17,456 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:290)
2009-04-11 11:43:17,456 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:206)
2009-04-11 11:43:17,456 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
2009-04-11 11:43:17,457 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:235)
2009-04-11 11:43:17,457 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:206)
2009-04-11 11:43:17,457 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
2009-04-11 11:43:17,457 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
2009-04-11 11:43:17,457 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.jboss.web.tomcat.security.SecurityAssociationValve.invoke
(SecurityAssociationValve.java:190)
2009-04-11 11:43:17,457 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
2009-04-11 11:43:17,458 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process
(SecurityContextEstablishmentValve.java:126)
2009-04-11 11:43:17,460 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke
(SecurityContextEstablishmentValve.java:70)
2009-04-11 11:43:17,460 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
2009-04-11 11:43:17,460 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
2009-04-11 11:43:17,461 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke
(CachedConnectionValve.java:158)
2009-04-11 11:43:17,461 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
2009-04-11 11:43:17,461 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
2009-04-11 11:43:17,461 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
2009-04-11 11:43:17,461 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process
(Http11Protocol.java:601)
2009-04-11 11:43:17,461 ERROR [STDERR] (http-127.0.0.1-10080-1) at
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
2009-04-11 11:43:17,461 ERROR [STDERR] (http-127.0.0.1-10080-1) at
java.lang.Thread.run(Thread.java:619)
if any body find a way ,please let me know.
thanks in advance