package org.qin.netbookstore;
import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
private Session session;
private Configuration config;
private SessionFactory factory;
private Transaction transaction;
public Test(){
config = new Configuration().configure();
factory = config.buildSessionFactory();
session = factory.openSession();
}
public void Inster(){
try {
transaction = session.beginTransaction();
Date insertDate = new Date(1982-10-10);
Date publishDate = new Date(1970-10-10);
Bookinfo book = new Bookinfo();
book.setName( "足球周刊 ");
book.setAuthor( "秦明 ");
book.setBookConcern( "体坛周报 ");
book.setWordCount(new Integer(56789));
book.setIsbn( "9771671073020250 ");
book.setInsertDate(insertDate);
book.setPublishDate(publishDate);
book.setProspectus( "fdasfdasfdashklfdkdasklfsdjafjklasd ");
session.save(book);
session.flush();
transaction.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(transaction == null){
transaction.rollback();
}
}finally{
session.close();
}
}
public static void main(String [] args){
Test test = new Test();
test.Inster();
}
}
一执行到transaction.commit()时就报NullPointException请教一下!
错误信息如下:
Hibernate: insert into netbookstore.bookinfo (name, author, bookConcern, publishDate, wordCount, prospectus, insertDate, isbn) values (?, ?, ?, ?, ?, ?, ?, ?)
Connection.java:2083 com.mysql.jdbc.Connection.clearWarnings(..) returning null
Exception in thread "main " java.lang.NullPointerException
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:610)
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:548)
at com.mysql.jdbc.PreparedStatement.toString(PreparedStatement.java:3987)
at java.lang.String.valueOf(String.java:2577)
at java.lang.StringBuffer.append(StringBuffer.java:220)
at com.mysql.jdbc.trace.Tracer.printParameters(Tracer.aj:240)
at com.mysql.jdbc.trace.Tracer.printEntering(Tracer.aj:167)
at com.mysql.jdbc.trace.Tracer.entry(Tracer.aj:126)
at com.mysql.jdbc.trace.Tracer.ajc$before$com_mysql_jdbc_trace_Tracer$1$f51c62b8(Tracer.aj:45)
at com.mysql.jdbc.Connection.registerStatement(Connection.java)
at com.mysql.jdbc.Statement. <init> (Statement.java:265)
at com.mysql.jdbc.PreparedStatement. <init> (PreparedStatement.java:467)
at com.mysql.jdbc.Connection.clientPrepareStatement(Connection.java:2186)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4766)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4671)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:396)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:76)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:69)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:150)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1839)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2200)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)