1

I'm having the error and I'm not understanding why there is no code below although I know what he means.

Error

Org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before cleaning: com.domain.Car.motor -> com.domain.Motor

I was having this error in another method, when I reported session.flush () and session.clear () the error propagated to this method now, I would understand not to have Fill in the session.flush () and session.clear () code.

I am not using cascade because motor is not to be saved along with car, I want to save engine inside car

The error occurs in the criteria in the sample method SaveSend(Carro carro);

Example of the problem

@Entity
@Table(name="carro")
public class Carro implements Serializable {

    @Id
    private Long codigo;
    private String nome;

    @ManyToOne(targetEntity=Motor.class)
    @JoinColumn(name="codigoMotor", referencedColumnName = "codigoMotor")
    private Motor motor;

    // get and set
}

@Entity
@Table(name="motor")
public class Motor implements Serializable {

    @Id
    private Long codigoMotor;
    private String nome;

    // get and set

}

public void salvar() {
    carro.setMotor(getMotor);
    dao.salvar(carro); // Saving the car Ok;
    depoisSalvar(carro);
}

public void depoisSalvar(carro) {
    salvarVenda(carro); 
}

// Saving the sale
public void salvarVenda(Carro carro) {
    // Before saving, go to DAO to find the buyer
    Criteria cri = getSession().createCriteria(Vendedor.class);
    cri.add(Restrictions.eq("carro.codigo", carro.getCodigo()));    
    return cri.list(); // Aqui acontece o erro....

    dao.salvar();
}
Ger
  • 583
  • 2
  • 13
  • 35
  • Possible duplicate of [Data was not saved: object references an unsaved transient instance - save the transient instance before flushing](http://stackoverflow.com/questions/17283431/data-was-not-saved-object-references-an-unsaved-transient-instance-save-the-t) – Nikos Paraskevopoulos Jan 04 '17 at 12:28
  • Thanks for the answer. I saw this post, in my case, I am not salvano Motor along with my object Car, I am inserting engine in my car object to save the car. – Ger Jan 04 '17 at 13:20
  • See this thread, for example: http://stackoverflow.com/questions/41225598/jpa-relation-manytomany-on-the-same-entity/41232342#41232342. Referencing transient entities results in errors when a query is executed – crizzis Jan 04 '17 at 18:53

0 Answers0