org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: cn.itcast.domain.Department

Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: cn.itcast.domain.Department
    at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
    at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:242)
    at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:597)
    at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3123)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:479)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:204)
    at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:127)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
    at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at cn.itcast.test.Mang2OneDemo.add(Mang2OneDemo.java:28)
    at cn.itcast.test.Mang2OneDemo.main(Mang2OneDemo.java:12)

object references an unsaved transient instance - save the transient instance before flushing: cn.itcast.domain.Department
对象依赖一个未保存的实体,保存这个实体在刷新之前。实体为“Department”。解决方案是:1、使用级联操作 2、在进行save操作之前先将关联的对象save一下

posted @ 2016-08-23 19:03  guodaxia  阅读(1913)  评论(0编辑  收藏  举报