学习hibernate映射的时候遇到如下异常:org.hibernate.TransientObjectException: object references an unsaved transient instance,最后排查出原因是因为,对象在new后,未能转换为持久态,我们在配置的时候将其变为持久态即可。下边详细说明
hibernate.TransientObjectException解决方案――工具/原料hibernate映射
hibernate.TransientObjectException解决方案――TransientObjectException异常详细:hibernate.TransientObjectException解决方案 1、
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.gis520.model.People
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:242)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:430)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:265)
at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:619)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3141)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:501)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:227)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:150)
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:49)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at com.gis520.test.CreateTableTest.testInsert(CreateTableTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
……
hibernate.TransientObjectException解决方案――异常解决方法:hibernate.TransientObjectException解决方案 1、
首先,查看<many-to-one>标签下是否添加了级联属性cascade,如果没有就添加,值为save-update,persist,如图
hibernate.TransientObjectException解决方案 2、
然后测试,从新运行程序,没有报错,证明已经成功
hibernate.TransientObjectException解决方案 3、
查看sql输出语句和log4j信息。
Hibernate: insert into People (pname, phone) values (?, ?)
Hibernate: insert into Emp (ename, job, sal, pid) values (?, ?, ?, ?)
两个表已经创建并且都执行的持久化操作。
hibernate.TransientObjectException解决方案_transient
hibernate.TransientObjectException解决方案 4、
数据库浏览,可见外键PID。异常成功解决。