'PersistenceException occured : org.hibernate.exception.ConstraintViolationException: could not insert: [models.User]

I have got this exception!! here is my model class

@Entity
public class User extends Model {

    @OneToMany(mappedBy="email", cascade=CascadeType.ALL)
    public List<Photo> photo;

    @Email
    @Required
    @Unique
    public String email;

    @Required
    public String passwordHash;

    @Required
    public String education;

    @Required
    public String fname;

    @Required
    public String lname;

    @Required
    public Date dob;

    @Required
    public String gender;

    @Required
    public String country;

    @Required
    public Long phone;

    @Required
    public String status;

    @Required
    public String jobtitle;

    @Required
    public String company;

    @Required
    public String industry;

    @Required
    public Date addDate;

    public String needConfirmation;

public User(String email, String password, String fname,
                String lname, Date dob, String gender, String country,
                Long phone, String status, String education, String jobtitle, String company, String industry) {
        //all initialization here
    }
}

can you please tell me where I am going wrong playframework× 2289


this is my photo class, and please tell me how can I allow JPA to generate my database


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import controllers.Users;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import play.db.jpa.Model;

/**
 *
 * @author nPandey
 */
@Entity
public class Photo extends Model{

    public String photoname;

    @ManyToOne
    public User email;

    public String owner;

    public Photo(){

    }

    public Photo(User email, String photo){
        this.photoname=photo;
        this.email=email;
        this.owner=email.email;
    }

}


Solution 1:[1]

Check that your entity isn't missing any required fields when it's being persisted / updated. Also check what constraints are set on the User-table in the DB (unique keys, foreign keys, non nulls...).

Solution 2:[2]

Your mappedBy value makes little sense. It is used on the passive end of a bi-directional association. In those cases it points to the attribute on the other class on the other side of the relationship. You are pointing it to one of the attributes of the User class. Perhaps your Photo class has a email attribute, but is that of type User ( i am guessing not). So if this is a bi-directional association, set the mappedBy value to the attribute on Photo having the corresponding @ManyToOne "back" to the User. If it is a unidirectional association, remove the mappedBy and perhaps use @JoinColumn to use the foreign key mapping strategy of unidirectional one-to-many

Solution 3:[3]

I was facing same issue and it got resolved by changing relationship related notation from OneToMany to ManyToMany. In My case there was ManyToMany relation between Job and Person Table and I was trying to add same jobList to multiple person object in a loop.

The problem was with the annotation used in Person Entity class for Job Entity:

@ManyToMany // Changed from OneToMany to ManyToMany
private List<Job> jobList = new ArrayList<Job>();

Solution 4:[4]

Try editing the code as below - In User Model

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    public List<Photo> photos;

In Photo Model

    @ManyToOne
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name = "email", nullable = false, insertable = false, updatable = false)
    public User user;

Check this for reference - http://en.wikibooks.org/wiki/Java_Persistence/OneToMany#Example_of_a_OneToMany_relationship_and_inverse_ManyToOne_annotations

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 esaj
Solution 2 raphaƫ?
Solution 3 Joe
Solution 4 developer9