0

I need to insert a null value in foreign key column, but I get this error: Is it possible?

Exception

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing: com.company.project.model.ConceptoGasto.conceptoGasto -> com.company.project.model.ConceptoGasto; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing: com.company.project.model.ConceptoGasto.conceptoGasto -> com.company.project.model.ConceptoGasto

Entity

//----------------------------------------------------------------------
// ENTITY PRIMARY KEY ( BASED ON A SINGLE FIELD )
//----------------------------------------------------------------------
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id", nullable=false)
private Integer id;

....

@ManyToOne (optional = true)
@JoinColumn(name="idPadre", referencedColumnName="id", nullable=true)
private ConceptoGasto conceptoGasto;

.....

Method

JpaOperation operation = new JpaOperation() {
 @Override
 public Object execute(EntityManager em) throws PersistenceException {
   T managedEntity = em.merge(entityToSave);
   //em.refresh(managedEntity);
   return managedEntity;
 }
 } ;
Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
  • See http://stackoverflow.com/questions/2302802/object-references-an-unsaved-transient-instance-save-the-transient-instance-be and http://stackoverflow.com/questions/17283431/data-was-not-saved-object-references-an-unsaved-transient-instance-save-the-t – Wim Deblauwe Apr 07 '16 at 18:24
  • Wim: The topics explain about two tables relation, but not for just one table. Some idea? Thank You – user1538369 Apr 07 '16 at 19:57
  • Is the field `private ConceptoGasto conceptoGasto;` contained inside the `ConceptoGasto` class? – Wim Deblauwe Apr 08 '16 at 06:30

0 Answers0