hibernate報幾種異常的及解決方法
異常1:
Exception in thread "main" org.hibernate.MappingException: Could not instantiate id generator [entity-name=com.ye.enty.Room]
at org.hibernate.id.factory.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:118)at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:193)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:248)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
at com.ye.test.Test.main(Test.java:21)
Caused by: org.hibernate.MappingException: Could not interpret id generator strategy [navtive]
at org.hibernate.id.factory.DefaultIdentifierGeneratorFactory.getIdentifierGeneratorClass(DefaultIdentifierGeneratorFactory.java:137)
at org.hibernate.id.factory.DefaultIdentifierGeneratorFactory.createIdentifierGenerator(DefaultIdentifierGeneratorFactory.java:108)
... 4 more
解決方法是:主鍵生成策略那裡把錯誤的navtive
<id name="roomId" type="java.lang.Integer">
<column name="ROOMID" />
<generator class="navtive" /><!--改為native-->
</id>
異常2:Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com.ye.enty.Room not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:799)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2344)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2243)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
at com.ye.test.Test.main(Test.java:20)
解決方法是:修正物件對映裡面的hibernate.cfg.xml的(兩種錯誤的寫法都展示)
<mapping resource="com.ye.enty.Room.hbm.xml"/>
<mapping resource="com.ye.enty.Student"/>
把裡面的 “.”點(不含引號)改為<mapping resource="com/ye/enty/Room.hbm.xml"/>和 <mapping resource="com/ye/enty/Student.hbm.xml"/>
異常3:Exception in thread "main" org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.ye.enty.Student
at org.hibernate.id.Assigned.generate(Assigned.java:53)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
at com.ye.test.Test.main(Test.java:25)
解決方法是:改為對應的資料庫主鍵生成方式,如MySql的,則改為native,Oracle的則改為sequence,裡面的引數改為對應的序列
異常4:Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.ye.enty.Room
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:265)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:275)
at org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:295)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3404)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:520)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:230)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:154)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
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 com.ye.test.Test.main(Test.java:27)
解決方法是:用將所有的自由態轉成session的遊離態。之後呼叫裡面的save();方法。
異常5:
是因為程式碼中有Transaction的巢狀,如:
Transaction tx1=session.beginTransaction()
Transaction tx2=session.beginTransaction()
tx2.commit();
tx1.commit();
在適用hibernate JPA的時候,不能適用事務巢狀