-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: using JPA in servlets
PostPosted: Sat Apr 11, 2009 2:30 am 
Newbie

Joined: Sat Apr 04, 2009 11:39 pm
Posts: 1
Location: Chennai,Tamilnadu,India
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

_________________
dtrprasad


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 5:59 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi, there are several problems here, the first one that you are posting in the wrong forum.
For JBoss5 EJB3 :
http://www.jboss.org/index.html?module=bb&op=viewforum&f=221
or for EntityManager:
http://forum.hibernate.org/viewforum.php?f=1
But I'd suggest the JBoss forum for this question.

Some more tips to get answers:
To understand a question about "NullPointer at line XX" you should post the line numbers; also formatting the stacktrace my help a lot.
Also when deploying to JBoss5 you don't need to configure c3p0 database connection pools, you should configure it in the datasource definition deployed to JBoss, read the datasource reference docs.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.