`
weihan1314
  • 浏览: 15383 次
  • 性别: Icon_minigender_1
  • 来自: 成都
最近访客 更多访客>>
社区版块
存档分类
最新评论

Hibernate错误提示以及解决方法总结

    博客分类:
  • J2EE
阅读更多
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

错误原因:主键的值不能重复,数据表中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>



另外提示一个小问题:有时候按下@时没有提示,可以在窗口--首选项--java--内容辅助(content assist)下面将自动激活设置如下:

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics