Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
727 views
in Technique[技术] by (71.8m points)

jpa - Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'

I would like to store birthdate so I chose date at MySQL, when I create my entities based in my database, it turns out like this:

import java.util.Date;

    // ..code
    @NotNull(message="fill you birthdate")
    @Temporal(TemporalType.DATE)
    private Date birthdate;

But when I try to persist it gives me this error:

Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'. Please refer to embedded ConstraintViolations for details.

What am I doing wrong here ? I was reading something about define time zone in Google, I'm from Brazil, how should I do that ?

EDIT

package entity;

import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;

import java.util.Date;
import java.util.List;


/**
 * The persistent class for the user database table.
 * 
 */
@Entity
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @Temporal(TemporalType.DATE)
    private Date birthdate;

    @NotNull(message="informe seu e-mail")
    @Email(message="e-mail inválido")
    private String email;

    @NotNull(message="informe seu gênero")
    private String gender;

    private String image;

    @NotNull(message="informe seu nome completo")
    private String name;

    @Size(min=6,max=16, message="senha com no mínimo: 6 dígitos e no máximo 16 dígitos")
    @NotNull(message="informe sua senha")
    private String password;

    //bi-directional many-to-one association to Document
    @OneToMany(mappedBy="user")
    private List<Document> documents;

    //bi-directional many-to-one association to QuestionQuery
    @OneToMany(mappedBy="user")
    private List<QuestionQuery> questionQueries;

    //bi-directional many-to-one association to Team
    @OneToMany(mappedBy="user")
    private List<Team> teams;

    public User() {
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Date getBirthdate() {
        return this.birthdate;
    }

    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGender() {
        return this.gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getImage() {
        return this.image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<Document> getDocuments() {
        return this.documents;
    }

    public void setDocuments(List<Document> documents) {
        this.documents = documents;
    }

    public List<QuestionQuery> getQuestionQueries() {
        return this.questionQueries;
    }

    public void setQuestionQueries(List<QuestionQuery> questionQueries) {
        this.questionQueries = questionQueries;
    }

    public List<Team> getTeams() {
        return this.teams;
    }

    public void setTeams(List<Team> teams) {
        this.teams = teams;
    }

    public void print() {
        System.out.println("User [id=" + id + ", birthdate=" + birthdate + ", email="
                + email + ", gender=" + gender + ", image=" + image + ", name="
                + name + ", password=" + password + "]");
    }



}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I got the same problem, but after hours looking for the answer, Finally I Found it.... You should edit your AbstractFacade.java class and add this code

public void create(T entity) {

    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<T>> constraintViolations = validator.validate(entity);
    if(constraintViolations.size() > 0){
        Iterator<ConstraintViolation<T>> iterator = constraintViolations.iterator();
        while(iterator.hasNext()){
            ConstraintViolation<T> cv = iterator.next();
            System.err.println(cv.getRootBeanClass().getName()+"."+cv.getPropertyPath() + " " +cv.getMessage());

            JsfUtil.addErrorMessage(cv.getRootBeanClass().getSimpleName()+"."+cv.getPropertyPath() + " " +cv.getMessage());
        }
    }else{
        getEntityManager().persist(entity);
    }
}

Now this method will alert you which property and why it fails the validation. I hope this works for you, as it does for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...