简体   繁体   中英

Domain table getting error “object references an unsaved transient instance - save the transient instance before flushing”

I've already seen the error "org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing" when I started to work with JPA. I know that this error on general is solved adding cascade=CascadeType.ALL, but in this case, this error have appeared in a domain table, that's means, doesn't make any sense put CascadeType in this case, because this entity already has an Id in the database.

The relevant part of my code is (there are another fields, getters, setters, etc):

Class Rac:

@Entity
@Table(name = "RAC", schema = "SchRAC")
public class Rac {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "RacCod")
private Long idRac;

//anoter fields (...)

@OneToMany(fetch = FetchType.LAZY, mappedBy = "rac", cascade = {CascadeType.ALL})
@JsonManagedReference 
private List<RacProvidencia> racProvidencias;

Class RacProvidencia:

@Entity
@Table(name = "RACPRO", schema = "SchRAC")
public class RacProvidencia implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "RacProCod")
    private Long idRacProvidencia;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "RacCod")
    @JsonBackReference //coloquei para evitar a recursao infinita de JSON
    private Rac rac;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
    @JoinColumn(name = "ProCod")
    private Providencia providencia;

    @NotNull
    @Column(name = "RacProNum", length = 100)
    private String numero;

    @Column(name = "RacProDat")
    private Date dataProvidencia;

Class Providencia (The domain table):

@Entity
@Table(name = "PRO", schema = "SchRAC")
public class Providencia {

    @Id
    @Column(name = "ProCod")
    private Integer idProvidencia;

    @Column(name = "ProDes", length = 50)
    private String descricao;

Service:

public RacProvidencia adicionarProvidenciaRac(Long codigo, @Valid RacProvidencia racProvidencia) {
    Rac rac = this.racRepository.findById(codigo).get();
    racProvidencia.setRac(rac);
    return racProvidenciaRepository.save(racProvidencia);
}

As you can see, the object "Rac" is already saved, then the user adds "Providencias" if he would like to do it. Then I receive the object "Providencia", search the object Rac by Id, set the object and save.

The strangest part of this error is because, after 5 minutes, the system doesn't show this error anymore. What could be happening? How could I avoid this problem in the future?

You have a bidirectional relationship but you only set one side of the relationship.

public RacProvidencia adicionarProvidenciaRac(Long codigo, @Valid RacProvidencia racProvidencia) {
    Rac rac = this.racRepository.findById(codigo).get();
    racProvidencia.setRac(rac);

    rac.getRacProvidencias().add(racProvidencia);  // <-- This

    return racProvidenciaRepository.save(racProvidencia);
}

Your persistence provider needs that on both sides.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM