第一次Hibernate之旅经验总结
1.错误提示:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
?atorg.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:96)
?atorg.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaReflectionManager.java:226)
?atorg.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
?atorg.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 (?, ?, ?)
Exceptioninthread"main"org.hibernate.SessionException:Sessionwasalreadyclosed
?atorg.hibernate.impl.SessionImpl.close(SessionImpl.java:320)
?atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)
?atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
?atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
?atjava.lang.reflect.Method.invoke(Method.java:589)
?atorg.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
?at$Proxy0.close(UnknownSource)
?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 (?, ?, ?)
Exceptioninthread"main"org.hibernate.exception.ConstraintViolationException:CouldnotexecuteJDBCbatchupdate
?atorg.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
?atorg.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
?atorg.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
?atorg.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
?atorg.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
?atorg.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
?atorg.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
?atorg.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
?atorg.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
?atorg.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
?atStudentTest.main(StudentTest.java:21)
Causedby:java.sql.BatchUpdateException:Duplicateentry'7'forkey'PRIMARY'
?atcom.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1693)
?atcom.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1108)
?atorg.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
?atorg.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
?... 8 more错误原因:主键的值不能重复,数据表中id=7的数据已经存在,所以导致插入数据失败!
?
4.错误提示(使用Session session = sf.getCurrentSession()):
Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
?atorg.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>