Hibernate程序碰到的两个问题
这个问题调了两天,最后发现是hibernate3.2.4的问题,转载一下,希望帮助遇到同样问题的同学。
http://hi.baidu.com/deimy/blog/item/62c1cc5cb6053741faf2c04f.html
一、Hibernate插入数据失败错误
错误信息:
init:
deps-module-jar:
deps-ear-jar:
deps-jar:
compile-single:
run-main:
log4j:WARNNoappenderscouldbefoundforlogger(org.hibernate.cfg.Environment).
log4j:WARNPleaseinitializethelog4jsystemproperly.
Hibernate:insertintouser(name,age,id)values(?,?,?)
Exceptioninthread"main"org.hibernate.exception.SQLGrammarException:couldnotinsert:[cn.deimy.User]
atorg.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
atorg.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
atorg.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
atorg.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
atorg.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
atorg.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
atorg.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
atorg.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
atorg.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
atorg.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
atorg.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
atorg.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
atorg.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
atorg.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
atorg.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
atorg.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
atcn.deimy.FirstHibernate.main(FirstHibernate.java:41)
Causedby:java.sql.SQLException:Novaluespecifiedforparameter3
atcom.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
atcom.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
atcom.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
atcom.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:1851)
atcom.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1665)
atcom.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1608)
atcom.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1593)
atorg.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
atorg.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
...16more
JavaResult:1
BUILDSUCCESSFUL(totaltime:5seconds)
这个错误是由Hibernate3.2.4自身存在的bug引起,具体介绍见:Since3.2.4novaluespecifiedforrowid,使用之前的老版本Hibernate或下载最近的Hibernate3.2.4-sp1即可解决问题。
参考资料:
http://dev.eclipse.org/newslists/news.eclipse.technology.emft/msg02664.html
Since3.2.4novaluespecifiedforrowid
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2605
http://www.guj.com.br/posts/list/59353.java
二、log4j没有配置的警告
警告信息:
log4j:WARNNoappenderscouldbefoundforlogger(org.hibernate.cfg.Environment).
log4j:WARNPleaseinitializethelog4jsystemproperly.
这是由于没有配置log4j.properties属性文件造成
把hibernate-3.2/etc目录下的log4j.properties文件复制到src目录(需要build项目后再运行)或直接copy到WEB-INF/classes目录下即可。
log4j.logger.org.hibernate=info|debug|error
log4j.properties属性文件的这一行配置控制台日志输出级别
QNjrsjIxo6dGJ7oTbl0Ard6VNnDHDSI7ras6QJCoMHAiO6jW7o4+uF1Vy5h6
P8VrpoZJjZjxI/nYNF0k6e03XaWXMtJBJCWdyXM6aEc7whCaM9sKk4MYIpVj
MgqpzWCxvtxFkukqM3hivz6LfpzBeH5nnhGMXz9T9jZj+yLGHCUW4QOS2kLr
xFjLI5uynKZRzHUgTFpSPm+hcHeTQnV2OQRWC7d9ShFMY4n0OjC7Tsbvd3Jk
cu4BmaSnsA/+TD9T3TCf5VossGBR6B7E9RWFNW+83gFjZrQpB4mPADI9k9Km
Iti1jTwswNmoy2tbzIo5PWnlXmcqjpxLOjySgaT9Gg==