when testing the call in postman it returns the object as if it were saved but the id comes with value zero, follow my code below
@Entity(name = "TB_CLIENTES")
public class Cliente implements Serializable {
private static final long serialVersionUID = -9073849407172730391L;
@Id
@Column(name = "CLI_ID", nullable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
// A tabela de Cliente se relaciona com Lojista,
// porém através do campo Cliente.CLI_LOJISTA <=> Logista.LOGIN_USER_LOGIN <=>
// LoginUser.LOGIN_USER_LOGIN,
// e não através do ID.
// O relacionamento não é adicionado da maneira correta aqui pois isso degrada a
// performance, bem visível nos logs de SQL do hibernate.
// @NotNull
// @ManyToOne
// @JoinColumn(name = "CLI_LOJISTA", nullable = false, referencedColumnName =
// "LOGIN_USER_LOGIN")
// private Lojista lojista;
@NotNull
@Column(name = "CLI_LOJISTA", nullable = false)
private String lojistaLogin;
@NotNull
@Column(name = "CLI_NOME", nullable = false)
private String nome;
@NotNull
@Column(name = "CLI_ENDERECO", nullable = false)
private String endereco;
@NotNull
@Column(name = "CLI_NUMERO", nullable = false)
private String numero;
@NotNull
@Column(name = "CLI_BAIRRO", nullable = false)
private String bairro;
@NotNull
@Column(name = "CLI_CIDADE", nullable = false)
private String cidade;
@NotNull
@Column(name = "CLI_UF", nullable = false)
private String uf;
@NotNull
@Column(name = "CLI_CEP", nullable = false)
private String cep;
@NotNull
@Column(name = "CLI_FONE1", nullable = false)
private String fone1;
@Column(name = "CLI_FONE2")
private String fone2;
@Column(name = "CLI_OBS")
private String obs;
@Column(name = "CLI_EMAIL")
private String email;
@NotNull
@Column(name = "CLI_RG", nullable = false)
private String rg;
@NotNull
@Column(name = "CLI_CPF", nullable = false)
private String cpf;
@Column(name="CLI_PHOTOCLI", nullable = true)
private String foto;
Controller interface and its implementation
@RequestMapping(value = "/new", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_JSON_VALUE})
ClientVm newCliente(@RequestBody Cliente edit)
throws BadRequestException, NotFoundException ;
@Override
public ClientVm newCliente(@RequestBody Cliente cli)throws BadRequestException, NotFoundException {
validator.validateCpf(cli.getCpf());
try {
return business.newCliente(cli);
} catch (IOException ex) {
Logger.getLogger(ClientServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
Business layer and repository (used JPARepository)
public ClientVm newCliente(Cliente cli) throws NotFoundException, FileNotFoundException, IOException {
LoginUser currentUser = clientBusiness.getCurrentUser();
cli.setLojistaLogin(currentUser.getLoginLojista());
//cli.setFoto(getPhotoBase64Image(cli.getId()));
String fileName = null;
String patch = null;
if (!cli.getFoto().equals("")){
byte[] data;
Date curDate = new Date();
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy-hhmmss");
fileName = "Logos/photocli/"+cli.getLojistaLogin()+"-"+cli.getCpf()+"-"+format.format(curDate)+".jpg";
patch = Constants.PHOTO_CLI_PATH +fileName;
data = Base64.getDecoder().decode(cli.getFoto().getBytes(StandardCharsets.UTF_8));
try (OutputStream stream = new FileOutputStream(patch)) {
stream.write(data);
}
System.out.println("Salvando base de dados em "+patch);
cli.setFoto("~/"+fileName);
}
Cliente cliRepo = clienteRepository.saveAndFlush(cli);
clienteRepository.flush();
ClientVm clienteReturn = new ClientVm(cliRepo);
//clienteReturn.setFoto(getPhotoBase64Image(cliRepo.getId()));
return clienteReturn;
}