java – com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败

当我想要插入一些数据时,我经常会出错项目类@Entity@NamedQueries({@NamedQuery(name = Project.findAll,query = SELECT p FROM Project p),@NamedQuery(name = Project.findByTitle,query = SELECT p FROM...

当我想要插入一些数据时,我经常会出错

项目类

@Entity
@NamedQueries({
        @NamedQuery(name = "Project.findAll",
         query = "SELECT p FROM Project p"),
        @NamedQuery(name = "Project.findByTitle",
                query = "SELECT p FROM Project p WHERE p.title = :title")
})
public class Project implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int projectId;

    @Column
    private String title;
    @Column
    private String description;
    @Column
    private Date projectDate;

    @ManyToOne
    @JoinColumn(name = "projectStatusId")
    private ProjectStatus projectStatus;

    @OneToMany(mappedBy = "projectMemberId",fetch = FetchType.EAGER)
    private List<ProjectMember> memberList = new ArrayList<ProjectMember>();

    /**
     * Setters and Getters
     */
}

ProjectMember类

@Entity
@NamedQueries({
        @NamedQuery(name = "ProjectMember.findAll",
                query = "SELECT pm FROM ProjectMember pm"),
        @NamedQuery(name = "ProjectMember.findByProject",
                query = "SELECT pm FROM ProjectMember pm WHERE pm.project = :project"),
        @NamedQuery(name = "ProjectMember.findByUser",
                query = "SELECT pm FROM ProjectMember pm WHERE pm.user = :user"),
        @NamedQuery(name = "ProjectMEmeber.findByUserAndProject",
                query = "SELECT pm FROM ProjectMember pm  WHERE pm.user = :user AND pm.project = :project")
})

public class ProjectMember implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int projectMemberId;


    @Column
    private Date activationDate;
    @Column
    private Date lastActiveDate;

    @ManyToOne
    @JoinColumn(name = "memberStatusId")
    private MemberStatus memberStatus;

    @ManyToOne
    @JoinColumn(name = "projectId")
    private Project project;


    @ManyToOne
    @JoinColumn(name = "memberRoleId")
    private MemberRole memberRole;

    @ManyToOne
    @JoinColumn(name = "userId")
    private User user;

    /**
     * Setter and Getter
     */
}

这就是我如何运行它

ProjectController类

public String createProject(){
    project.setProjectDate(new Date());
    project.setProjectStatus(new ProjectStatusFacade().findByStatus("active"));

    ProjectMember projectMember = new ProjectMember();
    projectMember.setMemberStatus(new MemberStatusFacade().findByStatus("accepted"));
    projectMember.setMemberRole(new MemberRoleFacade().findByRole("team leader"));
    projectMember.setActivationDate(new Date());
    projectMember.setUser(userSession.getUser());

    new ProjectFacade().create(project);
    System.out.print(project);
    projectMember.setProject(project);
    new ProjectMemberFacade().create(projectMember);

    userSession.setUser(new UserFacade().findById(userSession.getUser().getUserId()));
    return "index?faces-redirect=true";
}

这就是我最终得到的

 Caused by: javax.faces.el.EvaluationException: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
        ... more exceptions

    Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
        ... more exceptions

    Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
        ...more exceptions

    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`goent`.`projectmember`, CONSTRAINT `FKfyw2iinfhhsbrmqbu1sr7l93q` FOREIGN KEY (`projectMemberId`) REFERENCES `project` (`projectId`))
        ... more exceptions

    15:51:06,449 ERROR [io.undertow.request] (default task-4) UT005023: Exception handling request to /hibernate/pages/project-add-member.xhtml: javax.servlet.ServletException: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
        ... more exceptions

    Caused by: javax.faces.el.EvaluationException: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
        ... more exceptions

    Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
        ...more exceptions
    Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
        ... more exceptions

    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`goent`.`projectmember`, CONSTRAINT `FKfyw2iinfhhsbrmqbu1sr7l93q` FOREIGN KEY (`projectMemberId`) REFERENCES `project` (`projectId`))
        ... more exceptions

使用:Java,JPA,Hibernate,MySQL5.7

解决方法:

异常的原因是:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`goent`.`projectmember`, CONSTRAINT `FKfyw2iinfhhsbrmqbu1sr7l93q` FOREIGN KEY (`projectMemberId`) REFERENCES `project` (`projectId`))

您的操作违反了projectmember表中projectMemberId列的外键约束,该列根据您定义的规范引用项目表的projectId列.

本文标题为:java – com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败

基础教程推荐