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

錯誤原因:主鍵的值不能重複,數據表中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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章