当我想要插入一些数据时,我经常会出错项目类@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:无法添加或更新子行:外键约束失败
基础教程推荐
猜你喜欢
- 最新IntelliJ IDEA 2022配置 Tomcat 8.5 的详细步骤演示 2023-04-07
- SpringBoot统一功能处理的方式详解 2023-04-07
- JDK8中String的intern()方法实例详细解读 2023-05-25
- Springboot在有锁的情况下正确使用事务的实现代码 2023-08-07
- Spring MVC数据响应处理详解 2022-11-08
- Java连接Redis全过程讲解 2023-01-18
- Java Swing实现扫雷源码 2022-12-04
- JSP使用Servlet过滤器进行身份验证的方法 2023-08-03
- JSP中表达式的使用详解 2023-08-01
- SpringBoot中的ApplicationRunner与CommandLineRunner问题 2023-06-06