2013-01-23 89 views
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做同樣的方法?

回答

0

既然你要使用同一個會話對象,你可以使用會話的merge()方法。你可以參考的this

答案