-->
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.  [ 7 posts ] 
Author Message
 Post subject: problem with inserting values in database using hibernate
PostPosted: Wed Jun 21, 2006 10:13 am 
Newbie

Joined: Wed Jun 21, 2006 9:25 am
Posts: 5
Location: India
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hi ! i am new to hibernate i had read the quickstart guide of hibernate and write a small application using hibernate,which insert values in database;

Hibernate version:3.0

Mapping documents: Contact.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="net.sf.hibernate.examples.quickstart.Contact" table="CONTACT">
<id name="id" type="int" column="ID" >
<generator class="assigned"/>
</id>

<property name="firstName">
<column name="FIRSTNAME" />
</property>

<property name="lastName">
<column name="LASTNAME"/>
</property>

<property name="email">
<column name="EMAIL"/>
</property>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

HibernateUtil helper class:

HibernateUtil.java


package net.sf.hibernate.examples.quickstart;

import net.sf.hibernate.*;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Transaction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;
import net.sf.hibernate.cfg.Configuration;
import org.apache.commons.lang.*;
import org.apache.commons.lang.exception.NestableException;


public class HibernateUtil {

private static Log log = LogFactory.getLog(HibernateUtil.class);

private static SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory

sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
static ThreadLocal transaction = new ThreadLocal();

public static Session currentSession() {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
try {
s = sessionFactory.openSession();
session.set(session);
session.set(session);

} catch (HibernateException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}

Action Class


package net.sf.hibernate.examples.quickstart;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.util.Iterator;

import net.sf.hibernate.Session;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Transaction;


public class ContactAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res )throws Exception {


try
{

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/BARSDB");
if(ds!=null){
Connection conn = ds.getConnection();
}
Session session = HibernateUtil.currentSession();
Query query = session.createQuery("select c from Contact as c where c.id = :id"); query.setInteger("id", 2);
for (Iterator it = query.iterate(); it.hasNext();) {
Contact cat = (Contact) it.next();
System.out.println("First Name: " + cat.getFirstName() );
}


Contact princess =new Contact();
Transaction t = session.beginTransaction();

princess.setFirstName("Princess");
princess.setLastName("pri");
princess.setEmail("pri@yahoo.com");
princess.setId(1);
session.saveOrUpdate(princess);
t.commit();
session.close();
HibernateUtil.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
}
return mapping.findForward("success");
}
}



Full stack trace of any exception that occurs:



Now My problem is:

in the action u can see the select query.
this query is successfully selecting the data from table.
but when i m inserting values in table
if i commit the transaction by: t.commit();
then it gives java.lang.nullpointerException.
and if i commented this statement(t.commit()) then nothing happen ;
no exception,no error and no result in short no value in database is inserted.
Please help me getting out of this problem.
Thanks .


Name and version of the database you are using:

SAP DB

The generated SQL (show_sql=true):

No SQL is generated.

Debug level Hibernate log excerpt:

what is this?

please help me to come out of this problem.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 8:28 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The most obvious problem is that you're importing the Hibernate2 classes in your java file, but you say you're using hibernate3. You shoudn't be importing net.sf.hibernate, you need org.hibernate. Fix your imports, delete the hibernate 2 jar, and try again.

If that doesn't work, post the full exception. See if you can make the next post more legible, it's too hard to read your first post.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject: Not Solved yet..
PostPosted: Thu Jun 22, 2006 2:55 am 
Newbie

Joined: Wed Jun 21, 2006 9:25 am
Posts: 5
Location: India
Hi tenwit..
first of all i want to thank u for ur responce.
i did as u said.
i put hibernate3.jar file in my lib.
and imported following:

import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;


and now i am getting following Error


Last edited by krish4k on Thu Jun 22, 2006 4:00 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Please check this.
PostPosted: Thu Jun 22, 2006 3:57 am 
Newbie

Joined: Wed Jun 21, 2006 9:25 am
Posts: 5
Location: India
Hi tenwit..
first of all i want to thank u for ur responce.
i did as u said.
i put hibernate3.jar file in my lib.
and imported following:

import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;


and now i am getting following Error


SEVERE: Initial SessionFactory creation failed.
java.lang.NoClassDefFoundError: net/sf/cglib/core/KeyFactory
at org.hibernate.impl.SessionFactoryImpl.<clinit>(SessionFactoryImpl.java:308)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1054)
at net.sf.hibernate.examples.quickstart.HibernateUtil.<clinit>(HibernateUtil.java:25)
at net.sf.hibernate.examples.quickstart.ContactAction.execute(ContactAction.java:35)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:868)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:663)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)



i dint imported tis class any where. so why this problem.
please give me solution of this problem.
thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 8:12 am 
Regular
Regular

Joined: Wed Jan 11, 2006 12:49 pm
Posts: 64
Location: Campinas, Brazil
Dude, Hibernate has some dependencies. You have to add the jars that are bundled with Hibernate distribution (check the "lib" directory in the package you downloaded) to your application's classpath.

_________________
Henrique Sousa
Don't forget to rate useful responses


Top
 Profile  
 
 Post subject: one more possible point of error
PostPosted: Mon Jun 26, 2006 1:45 am 
Newbie

Joined: Wed Jun 21, 2006 9:25 am
Posts: 5
Location: India
now i have warning on server

Jun 26, 2006 10:58:15 AM net.sf.ehcache.config.Configurator configure
WARNING: No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/apache-tomcat-5.5.12/webapps/quickstart/WEB-INF/lib/ehcache-0.7.jar!/ehcache-failsafe.xml
Jun 26, 2006 10:58:16 AM org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
Jun 26, 2006 10:58:16 AM org.hibernate.impl.SessionFactoryImpl checkNamedQueries
INFO: Checking 0 named queries


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 26, 2006 7:25 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That's all good. You can make the config file warning go away by copying ehcache.xml from the etc directory of the hibernate distribution into the root of your classpath. The JNDI thing just means that you're not using JNDI, so that's fine. And you haven't written any named queries, so that's why you have the last line. You can turn off those infos by adding this line to your log4j.properties:
Code:
log4j.logger.org.hibernate.impl=WARN

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.