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.