0
I have validation problem in the Address class that is @Embeddable
while trying to make a POST
.
When I put on null
in the field nome
that is of Pessoa
for example, I can capture the custom message so far ok! But when I put null
in any class attribute Endereco
who is @Embeddable
I can’t validate and show custom message.
Follow the error:
{
"timestamp": "2021-04-15T01:34:19.071+00:00",
"status": 500,
"error": "Internal Server Error",
"trace": "org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement\n\tat org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:276)\n\tat org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233)\n\tat org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551)\n\tat org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)\n\tat org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)\n\tat org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)\n\tat org.springframework.data.jpa.repository.support. (ResultSetReturnImpl.java:197)\n\t... 111 more\n",
"message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
"path": "/pessoas"
}
Follow an example from my JSON, and the classes Pessoa
and Endereco
:
{
"nome": "Dayson Rodrigues Mota",
"ativo": true,
"endereco": {
"logradouro": null,
"numero": 90,
"complemento": "324",
"bairro": "Testeeee",
"cep": "234",
"cidade": "234",
"estado": "ld"
}
}
package com.example.algamoney.model;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Entity
@Table(name = "pessoa" )
public class Pessoa {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long codigo;
@NotNull
@Size(min = 10,max = 60)
private String nome;
@NotNull
private boolean ativo;
@Embedded
private Endereco endereco;
public Long getCodigo() {
return codigo;
}
public void setCodigo(Long codigo) {
this.codigo = codigo;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public boolean isAtivo() {
return ativo;
}
public void setAtivo(boolean ativo) {
this.ativo = ativo;
}
public Endereco getEndereco() {
return endereco;
}
public void setEndereco(Endereco endereco) {
this.endereco = endereco;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (ativo ? 1231 : 1237);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pessoa other = (Pessoa) obj;
if (ativo != other.ativo)
return false;
return true;
}
}
package com.example.algamoney.model;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Embeddable
public class Endereco {
@NotNull
@Size(min = 2, max = 60)
private String logradouro;
@NotNull
@Size(min = 1, max = 5)
private String numero;
@Size(min = 1, max = 10)
private String complemento;
@NotNull
@Size(min = 3, max = 50)
private String bairro;
@NotNull
@Size(min = 4, max = 9)
private String cep;
@NotNull
@Size(min = 2, max = 30)
private String cidade;
@NotNull
@Size(min = 2, max = 2)
private String estado;
public String getLogradouro() {
return logradouro;
}
public void setLogradouro(String logradouro) {
this.logradouro = logradouro;
}
public String getNumero() {
return numero;
}
public void setNumero(String numero) {
this.numero = numero;
}
public String getComplemento() {
return complemento;
}
public void setComplemento(String complemento) {
this.complemento = complemento;
}
public String getBairro() {
return bairro;
}
public void setBairro(String bairro) {
this.bairro = bairro;
}
public String getCep() {
return cep;
}
public void setCep(String cep) {
this.cep = cep;
}
public String getCidade() {
return cidade;
}
public void setCidade(String cidade) {
this.cidade = cidade;
}
public String getEstado() {
return estado;
}
public void setEstado(String estado) {
this.estado = estado;
}
}
Ball Show worked! Now only a doubt, in the case of the message to the user, it appears like this when I put null; "msgUsuario": "endereco.cep must have between 4 and 9 characters." In case how do I get only the field within the Address Class?
– dayson rodrigues
I didn’t understand the question, could elaborate a little more?
– nullptr
Anyway, @daysonrodrigues don’t forget to mark the answer as correct :)
– nullptr
I have placed a @Size(min 9,max=10) in the ZIP attribute, but when I put a smaller size as informed, it shows a custom message that I created through a file, but the informed message is cep address must have between 4 and 9 characters. How do I show only the ZIP attribute? In my file I created Validation.properties I put the message like this: Avax.validation.constraints.Size.message={0} must have between {min} and {max} caract u00e9res.
– dayson rodrigues