java 发生持久性异常:org.hibernate.exception.ConstraintViolationException:无法插入:[models.User]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10671050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:02:16  来源:igfitidea点击:

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

javahibernateplayframework

提问by Bj bhatta

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

你能告诉我我哪里出错了 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;
    }

}

回答by esaj

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...).

检查您的实体在持久化/更新时是否缺少任何必填字段。还要检查在数据库中的用户表上设置了哪些约束(唯一键、外键、非空值...)。

回答by rapha?λ

Your mappedByvalue 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 Userclass. Perhaps your Photo class has a emailattribute, but is that of type User( i am guessing not). So if this is a bi-directional association, set the mappedByvalue to the attribute on Photohaving 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

你的mappedBy价值毫无意义。它用于双向关联的被动端。在这些情况下,它指向关系另一端的另一个类的属性。您将它指向User类的属性之一。也许你的 Photo 类有一个email属性,但是是类型的User(我猜不是)。因此,如果这是双向关联,请将mappedBy值设置为Photo具有相应@ManyToOne“返回”到User. 如果是单向关联,去掉mappedBy,或者使用@JoinColumn 使用单向一对多的外键映射策略

回答by Abhishek Srivastava

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

我遇到了同样的问题,它通过将关系相关的符号从 更改为OneToMany来解决ManyToMany。在我的例子中ManyToMany,Job 和 Person Table 之间存在关系,我试图在循环中将相同的 jobList 添加到多个 person 对象。

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

问题出在 Person Entity 类中用于 Job Entity 的注释:

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

回答by developer9

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

检查此以供参考 - http://en.wikibooks.org/wiki/Java_Persistence/OneToMany#Example_of_a_OneToMany_relationship_and_inverse_ManyToOne_annotations