1
我在 http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/objectstate.html休眠:使瞬態對象持久化與會話
使用Hibernate Session看到使對象持久化
但我不知道這是否意味着從字面上使用會話。說明: 保存實例時,我收到以下錯誤:
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: xxxxxxx.entities.Sujeto.progTtipoSujeto -> xxxx.entities.TipoSujeto; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: xxxxx.entities.Sujeto.progTtipoSujeto -> xxxxx.entities.TipoSujeto
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:651)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:92)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:460)
我知道原因是Sujeto.progTtipoSujeto的類型是TipoSujeto的,它只是被保存Sujeto實例之前實例化。此外,現場progTtipoSujeto是關節標識符字段:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "IDN_TIPO_SUJETO", insertable = false, updatable = false)
private TipoSujeto progTtipoSujeto;
@Column(name = "IDN_TIPO_SUJETO")
private Integer idnTipoSujeto;
我一直在考慮(它的工作)是在保存之前從數據庫中獲取它的解決方案:
parserSujetoToEntity.parserSujetoToEntity(sujetoDTO, sujetoEntity);
TipoSujeto tipoSujetoEntity = (TipoSujeto) this.findByPrimaryKey(TipoSujeto.class, sujetoDTO.getTipo());
sujetoEntity.setProgTtipoSujeto(tipoSujetoEntity);
this.save(sujetoEntity);
是否有使用Session做同樣的方法?