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:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into user (name, age, id) values (?, ?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [cn.deimy.User]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at cn.deimy.FirstHibernate.main(FirstHibernate.java:41)
Caused by: java.sql.SQLException: No value specified for parameter 3
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:1851)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1665)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1608)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1593)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 16 more
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)
这个错误是由Hibernate 3.2.4自身存在的bug引起,具体介绍见:Since 3.2.4 no value specified for row id,使用之前的老版本Hibernate或下载最近的Hibernate 3.2.4-sp1即可解决问题。
参考资料:
http://dev.eclipse.org/newslists/news.eclipse.technology.emft/msg02664.html
Since 3.2.4 no value specified for row id
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2605
http://www.guj.com.br/posts/list/59353.java
二、log4j没有配置的警告
警告信息:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
这是由于没有配置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==