Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance -

Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.cat.po.proReport.Enterprise
 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:3151)
 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:1027)
 at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
 at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
 at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:655)
 ... 76 more

 

原因是:代码中关联的对象没有set值进去。
查看代码,发现是enterPriseStrength和enterprise为多对一关联,
代码直接enterPriseStrengthDao.saveOrUpdate(enterPriseStrength);//保存或更新
保存或更新前必须将enterprise的值set进去:enterPriseStrength.setEnterprise(enterprise)
如果是空值,则也必须set:enterPriseStrength.setEnterprise(null);

set值之后保存成功~~记住,保存一个对象的时候,所有关联的对象都必须set值,否则报错。

 

你可能感兴趣的:(Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance -)