hibernate 发生持久性异常:组织,休眠,异常,约束违反异常:无法插入:[型号.用户]

wb1gzix0  于 2022-12-19  发布在  其他
关注(0)|答案(4)|浏览(167)

我得到了这个异常!!这是我的模型类

@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
    }
}

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

}
5cg8jx4n

5cg8jx4n1#

检查你的实体在被持久化/更新时没有丢失任何必填字段。还要检查在DB中的用户表上设置了什么约束(唯一键、外键、非空值...)。

k4ymrczo

k4ymrczo2#

你的mappedBy值没有什么意义。它被用在双向关联的被动端。在这种情况下,它指向关系另一端的另一个类的属性。你将它指向User类的属性之一。也许你的Photo类有一个email属性,而是User型的(我猜不是)。因此,如果这是双向关联,则将mappedBy值设置为Photo上的属性,该属性使对应的@ManyToOne“返回”到User。如果是单向关联,删除mappedBy,并可能使用@JoinColumn来使用单向一对多的外键Map策略

2mbi3lxu

2mbi3lxu3#

我遇到了同样的问题,通过将关系相关的表示法从OneToMany更改为ManyToMany,解决了这个问题。在我的案例中,Job和Person表之间存在ManyToMany关系,我试图将相同的jobList添加到循环中的多个person对象。
问题出在作业实体的人员实体类中使用的注解:

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

bejyjqdl4#

尝试按以下方式编辑代码-在用户模型中

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

在照片模型中

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

请查看此内容以供参考-http://en.wikibooks.org/wiki/Java_Persistence/OneToMany#Example_of_a_OneToMany_relationship_and_inverse_ManyToOne_annotations

相关问题