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.(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单词改成native,如:

 
           
           

       


异常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的(两种错误的写法都展示)

   
       

把里面的  “.”点(不含引号)改为和 


异常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:

出现org.hibernate.TransactionException: Transaction not successfully started的原因 


是因为代码中有Transaction的嵌套,如:

Transaction tx1=session.beginTransaction()

         Transaction tx2=session.beginTransaction()

         tx2.commit();

tx1.commit();

在适用hibernate JPA的时候,不能适用事务嵌套

你可能感兴趣的:(Hibernate)