java 错误对象引用未保存的 transient 示例-即使CascadeType为ALL,也要在刷新前保存 transient 示例

lztngnrs  于 2023-04-19  发布在  Java
关注(0)|答案(1)|浏览(121)

我试图保存一个对象,但我得到these错误:

java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.example.magra.erp.models.entity.ventas.OrdenVenta.contacto -> com.example.magra.erp.models.entity.maestro.ClienteContacto

java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.example.magra.erp.models.entity.ventas.OrdenVenta.cliente -> com.example.magra.erp.models.entity.maestro.Cliente

这三个实体是:

奥登文塔酒店

@Entity
@Table(name="ven_orden_venta")
public class OrdenVenta implements Serializable {
    ...

    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler, contactos"}, allowSetters = false)
    private Cliente cliente;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler"}, allowSetters = false)
    private ClienteContacto contacto;
   
    ...
 
    //getters and setters
}

客户端

@Entity
@Table(name="mae_cliente")
public class Cliente implements Serializable {

    ...
    @JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "cliente_id")
    private List<ClienteContacto> contactos;
    ...
 
    //getters and setters
}

客户联系方式

@Entity
@Table(name="mae_cliente_contacto")
public class ClienteContacto implements Serializable {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    
    @Column(length=200)
    private String nombres;
    
    @Column(length=150)
    private String apellidoPaterno;
    
    @Column(length=150)
    private String apellidoMaterno;
    
    @Column(length=15)
    private String telefono;
    
    @Column(length=15)
    private String celular;
    
    @Column(length=250)
    private String correo;

    private Integer idUsuarioCrea;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date fechaCrea;

    private Integer idUsuarioModifica;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date fechaModifica;
 
    //getters and setters
}

我不知道我错在哪里。先谢谢你。
搜索一个解决方案,我发现使用CascadeType.ALL解决了这个错误。但这不是我的情况。
我使用了CascadeType.ALL并删除了该表以再次创建它,但这并没有解决错误。

2vuwiymt

2vuwiymt1#

首先将Cliente和ClienteContacta保存到实体中,并将创建的对象分配给OrdenVenta对象,解决了问题

@PostMapping("/orden-venta/create")
public ResponseEntity<?> create(@RequestBody OrdenVenta ordenVenta){
    Map<String, Object> response = new HashMap<>();
    
    try {
        Cliente cliente = clienteService.save(ordenVenta.getCliente());
        ordenVenta.setCliente(cliente);
        ClienteContacto contacto = contactoService.findByCorreo(ordenVenta.getContacto().getCorreo());
        ordenVenta.setContacto(contacto);
        ordenVentaService.save(ordenVenta);
    } catch (Exception e) {
        e.printStackTrace();
        response.put("ind", 0);
        return new ResponseEntity<Map<String, Object>>(response, HttpStatus.CONFLICT);
    }

    response.put("ind", 1);
    return new ResponseEntity<Map<String, Object>>(response, HttpStatus.OK);
}

相关问题