0
I’m using Spring and Hibernate in one of the applications I’m working on and I have a problem with processing transactions.
I need to do a POST using 2 entities, the Lancamentoentity and Usuarioentity, where the release has the user_id. When I tried to use with "optional = true" was going, trying to pass the User id to the release.setIdUsuarioEntity() the error in Postman is this:
Erro no método postLancamento(): Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
Call on the Postman:
http://localhost:8080/postLancamento/2
Dice:
{
"descricao": "Teste sdad adsuygh dsahudashuasdhu iadshuidashuidas ",
"valor": 780
}
My method in the controller:
@PostMapping("/postLancamento/{usuario_id}")
public String postLancamento(@PathVariable(value = "usuario_id") Long usuario_id, @Valid @RequestBody LancamentoEntity lancamentoEntity,
UsuarioEntity usuarioEntity) {
try {
usuarioEntity = new UsuarioEntity();
usuarioEntity.setId(usuario_id);
usuarioRepository.save(usuarioEntity);
// usuarioEntity.getId();
lancamentoEntity = new LancamentoEntity();
lancamentoEntity.setUsuarioEntity(usuarioEntity);
lancamentoRepository.save(lancamentoEntity);
return usuarioEntity.toString();
} catch (
Exception e) {
return "Erro no método postLancamento(): " + e.getMessage();
}
}
Lancamentoentity:
package com.webservice.msi.model;
@Entity
@Table(name = "lancamento")
public class LancamentoEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private Long id;
@Column(name = "data_de_lancamento")
private LocalDateTime data_de_lancamento;
@Column
private String descricao;
@Column
private Float valor;
// @JoinColumn(name = "usuario_id")
@ManyToOne( cascade = CascadeType.ALL, optional = false)
private UsuarioEntity usuario;
public LancamentoEntity() {
}
public LancamentoEntity(UsuarioEntity usuario, LocalDateTime data_de_lancamento, String descricao, Float valor) {
this.usuario = usuario;
this.data_de_lancamento = data_de_lancamento;
this.descricao = descricao;
this.valor = valor;
}
public UsuarioEntity getUsuarioEntity() {
return usuario;
}
public void setUsuarioEntity(UsuarioEntity ue) {
this.usuario = ue;
}
public LancamentoEntity(UsuarioEntity ueId) {
this.usuario = ueId;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescricao() {
return this.descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public Float getValor() {
return this.valor;
}
public void setValor(Float valor) {
this.valor = valor;
}
public LocalDateTime getData_de_lancamento() {
return this.data_de_lancamento;
}
public void setData_de_lancamento(LocalDateTime data_de_lancamento) {
this.data_de_lancamento = data_de_lancamento;
}
}
User entity:
@Entity
@Table(name = "usuario")
public class UsuarioEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Size(max = 30)
private String nome;
@NotBlank(message = "Por favor, insira um e-mail")
@Size(max = 50)
private String email;
@Size(max = 50)
private String senha;
@OneToOne(fetch = FetchType.LAZY, optional = false)
private ContaEntity conta;
@OneToMany(mappedBy = "usuario", cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
private List<LancamentoEntity> lancamentos;
public UsuarioEntity() {
}
public UsuarioEntity(Long id) {
this.id = id;
}
public UsuarioEntity(String nome, String email, String senha) {
this.nome = nome;
this.email = email;
this.senha = senha;
}
public UsuarioEntity(String nome, String email, String senha, ContaEntity contaEntity) {
this.nome = nome;
this.email = email;
this.senha = senha;
this.conta = contaEntity;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return this.nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSenha() {
return this.senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public List<LancamentoEntity> getLancamento() {
return lancamentos;
}
public void setLancamento(List<LancamentoEntity> lancamentos) {
this.lancamentos = lancamentos;
}
public ContaEntity getIdContaUsuario() {
return this.conta;
}
public void setIdContaUsuario(ContaEntity contaEntity) {
this.conta = contaEntity;
}
public String toString() {
return "{" + " id='" + getId() + "'" + ", name='" + getNome() + "'" + ", lastName='" + getSenha() + "'"
+ ", email='" + getEmail() + "'" + ", conta ='" + getIdContaUsuario() + "'" + "}";
}
}
The error in the terminal I am realizing is that I am not correctly passing only the Usuarioentity.getId():
Completed initialization in 6 ms Hibernate: select usuarioent0_.id as id1_2_1_, usuarioent0_.conta_id as conta_id5_2_1_, usuarioent0_.email as email2_2_1_, usuarioent0_.nome as nome3_2_1_, usuarioent0_.senha as senha4_2_1_, lancamento1_.usuario_id as usuario_5_1_3_, lancamento1_.id as id1_1_3_, lancamento1_.id as id1_1_0_, lancamento1_.data_de_lancamento as data_de_2_1_0_, lancamento1_.descricao as descrica3_1_0_, lancamento1_.usuario_id as usuario_5_1_0_, lancamento1_.valor as valor4_1_0_ from usuario usuarioent0_ left outer join lancamento lancamento1_ on usuarioent0_.id=lancamento1_.usuario_id where usuarioent0_.id=?
Could show the entire code of your Entity classes?
– mbissonho
@I edited with Entities.
– Everton Levi