第一次Hibernate之旅经验总结

1.错误提示:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
?at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:96)
?at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:226)
?at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
?at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1826)
?at StudentTest.main(StudentTest.java:17)

原因:没有引入hibernate-distribution-3.6.0.Final\lib\jpa下面的hibernate-jpa-2.0-api-1.0.0.Final.jar

?

2.错误提示:

Hibernate: insert into Student (name, age, id) values (?, ?, ?)
Exception in thread "main" org.hibernate.SessionException: Session was already closed
?at org.hibernate.impl.SessionImpl.close(SessionImpl.java:320)
?at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
?at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
?at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
?at java.lang.reflect.Method.invoke(Method.java:589)
?at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
?at $Proxy0.close(Unknown Source)
?at StudentTest.main(StudentTest.java:22)

原因:在程序中有这么一句Session session = sf.getCurrentSession();将其改为Session session = sf.openSession();问题解决,或者用if(session.isOpen())判断一下,具体原因不是很清楚,读者可以参考http://rmn190.javaeye.com/blog/370864

?

3.错误提示:

Hibernate: insert into Student (name, age, id) values (?, ?, ?)
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
?at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
?at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
?at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
?at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
?at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
?at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
?at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
?at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
?at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
?at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
?at StudentTest.main(StudentTest.java:21)
Caused by: java.sql.BatchUpdateException: Duplicate entry '7' for key 'PRIMARY'
?at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1693)
?at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1108)
?at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
?at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
?... 8 more

扫描二维码关注公众号,回复: 846580 查看本文章

错误原因:主键的值不能重复,数据表中id=7的数据已经存在,所以导致插入数据失败!


?

4.错误提示(使用Session session = sf.getCurrentSession()):

Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
?at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:683)
?at StudentTest.main(StudentTest.java:18)

错误原因:从3.0.1版本开始,Hibernate增加了 SessionFactory.getCurrentSession()方法。一开始,它假定了采用 JTA事务, JTA事务定义了当前session的范围和上下文(scope and context)。

错误主要原因是在hibernate.cfg.xml文件中忘记进行了如下设置:hibernate.current_session_context_class

如果是在web容器中运行hibernate,则在hibernate.cfg.xml中加入这句话:

<propertyname="hibernate.current_session_context_class">jta</property>

如果是在一个单独的需要进行JDBC连接的javaapplication中运行hibernate,则这样设置:

<propertyname="hibernate.current_session_context_class">thread</property>

猜你喜欢

转载自smilemonkey.iteye.com/blog/1027989