1

I've seen in http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/objectstate.html

Use the Hibernate Session to make an object persistent

But I don't know if it means literally using Session. Explanation: I get the following error when saving an instance:

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)

I know the reason is that Sujeto.progTtipoSujeto is of type TipoSujeto, and it's just been instantiated before saving the Sujeto instance. Besides, the field progTtipoSujeto is joint to an identifier field:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "IDN_TIPO_SUJETO", insertable = false, updatable = false)
private TipoSujeto progTtipoSujeto;
@Column(name = "IDN_TIPO_SUJETO")
private Integer idnTipoSujeto;

The solution I've been given (it works) is to fetch it from the database before saving:

parserSujetoToEntity.parserSujetoToEntity(sujetoDTO, sujetoEntity);
TipoSujeto tipoSujetoEntity = (TipoSujeto) this.findByPrimaryKey(TipoSujeto.class, sujetoDTO.getTipo());
sujetoEntity.setProgTtipoSujeto(tipoSujetoEntity);
this.save(sujetoEntity);

Is there a way to do the same using Session?

Luis Sep
  • 2,384
  • 5
  • 27
  • 33

1 Answers1

0

Since you are trying to use the same session object, you can use merge() method of session. You can refer answer of this

Community
  • 1
  • 1
DarkHorse
  • 2,740
  • 19
  • 28