Com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update child row: Foreign key constraint is not satisfied

I keep getting an error when I want to insert some data

Project class

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

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

      

that i run it

ProjectController class

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";
}

      

and what i get at the end

 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

      

Usage: Java, JPA, Hibernate, MySQL5.7

+3


source to share


3 answers


Thanks people, I was finally able to fix the error. The problem was in the Project class , it was about an incorrect mapping. Instead of specifying a variable to display in another class, I pointed to the PK of that class.

I wrote this

Project class

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

      



instead of this

//Correct code
@ManyToOne
    @JoinColumn(name = "project")
    private ProjectStatus projectStatus;

      

after this change the code started working well.

0


source


The reason for the exclusion is:

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

      



Your action violates a foreign key constraint on the projectMemberId column in the projectmember table that refers to the projectId column of the project... strong> according to your specific specifications.

0


source


This is most likely happening.

In the object, Project

you tell the persistence provider to always readily fetch ProjectNames

:

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

      

Then in the save method you create ProjectName

and populate it with Project

. Exactly, although the list in Project

does not contain the newly created one ProjectName

.

The dependency needs to be installed on both sides, so you end up with the following steps:

project.getMemberList().add(projectName);

      

0


source







All Articles