Javascript 如何使用SpringJPA和jQueryAjax构建和发送实体?
所以 我有这个客户端实体(删除了一些参数以缩短它): 这是我在控制器中的POST请求Javascript 如何使用SpringJPA和jQueryAjax构建和发送实体?,javascript,ajax,spring,spring-boot,Javascript,Ajax,Spring,Spring Boot,所以 我有这个客户端实体(删除了一些参数以缩短它): 这是我在控制器中的POST请求 @PostMapping("/save") public ResponseEntity<Cliente> save(@RequestBody Cliente cliente) { Cliente obj = clienteRepo.save(cliente); return new ResponseEntity<Cliente>
@PostMapping("/save")
public ResponseEntity<Cliente> save(@RequestBody Cliente cliente) {
Cliente obj = clienteRepo.save(cliente);
return new ResponseEntity<Cliente>(obj, HttpStatus.OK);
}
我见过另一种方法,我可以简单地编写一个json对象并发送它,但我不确定如何做到这一点
下面我离开location类以防万一
public class Ubicacion {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int ID_Ubicacion;
@Column(nullable = false)
private String calle;
@ManyToOne
@JoinColumn(name = "nombreSector", nullable = false)
private Sector sector;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "ubicacion")
private Cliente cliente;
}
因此,我认为我需要创建一个新的位置,并确保在此过程中不创建新的扇区(所有扇区都已在数据库中)。同样值得一提的是,部门之间也有这样的关系:部门>省份>国家。
对不起,如果这个问题太长,我只是尽量不遗漏任何信息
app.save({
id_Cliente : $('#id_Cliente').val(),
nombreCompleto: $('#nombreCompleto').val(),
sexo: $('#sexo').val(),
ubicacion.calle: $('#calle').val(), **this 2 lines are obviously wrong**
ubicacion.casa: $('#casa').val()
});
save : function(data){ //api call save
$.ajax({
url: app.backend + '/clientes/save',
data : JSON.stringify(data),
method: 'POST',
dataType : 'json',
contentType: 'application/json; charset=utf-8',
success : function(json){
$('#msg').text('Se guardo correctamente');
$('#msg').show();
$('#clienteModal').modal('hide');
app.table.ajax.reload();
},
error : function (error){
$('#msg').text(error.error);
$('#msg').show();
}
})
},
public class Ubicacion {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int ID_Ubicacion;
@Column(nullable = false)
private String calle;
@ManyToOne
@JoinColumn(name = "nombreSector", nullable = false)
private Sector sector;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "ubicacion")
private Cliente cliente;
}