java - Hibernate和mysql错误: Cannot add or update a child row

标签 java mysql sql hibernate foreign-keys

我有一个 hibernate 错误:

15:32:48,554 DEBUG SQL:111 - insert into apurement.user (groupe_id, username, password, email) values (?, ?, ?, ?)
15:32:48,664 WARN JDBCExceptionReporter:100 - SQL Error: 1452, SQLState: 23000
15:32:48,664 ERROR JDBCExceptionReporter:101 -
Cannot add or update a child row: a foreign key constraint fails (apurement.user,
CONSTRAINT groupe_id FOREIGN KEY (groupe_id) REFERENCES groupe (groupe_id)
ON DELETE NO ACTION ON UPDATE NO ACTION)

用于 session 管理的代码是Logic.java:

package com.beans;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.*;

public class Logic {
protected Configuration cfg;
protected SessionFactory sfg;
protected Session s;
protected Transaction tx;

public Logic() {

    this.init();

}

public void init() {
    this.setCfg(new Configuration().configure());
    this.setSfg(this.getCfg().buildSessionFactory());
    this.setS(this.getSfg().openSession());
    this.setTx(this.getS().beginTransaction());
}

public Configuration getCfg() {
    return cfg;
}

public void setCfg(Configuration cfg) {
    this.cfg = cfg;
}

public SessionFactory getSfg() {
    return sfg;
}

public void setSfg(SessionFactory sfg) {
    this.sfg = sfg;
}

public Session getS() {
    return s;
}

public void setS(Session s) {
    this.s = s;
}

public Transaction getTx() {
    return tx;
}

public void setTx(Transaction tx) {
    this.tx = tx;
}

}

用户类:

 package com.beans;

 // Generated 3 janv. 2013 12:07:19 by Hibernate Tools 3.4.0.CR1

/**
* User generated by hbm2java
 */
  public class User implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private Integer userId;
private Groupe groupe;
private String username;
private String password;
private String email;

public User() {
}

public User(Groupe groupe, String username, String password, String email) {
    this.groupe = groupe;
    this.username = username;
    this.password = password;
    this.email = email;
}

public Integer getUserId() {
    return this.userId;
}

public void setUserId(Integer userId) {
    this.userId = userId;
}

public Groupe getGroupe() {
    return this.groupe;
}

public void setGroupe(Groupe groupe) {
    this.groupe = groupe;
}

public String getUsername() {
    return this.username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return this.password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getEmail() {
    return this.email;
}

public void setEmail(String email) {
    this.email = email;
}

 }

用户.hbm:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 3 janv. 2013 12:07:19 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="User" table="user" catalog="apurement">
    <id name="userId" type="java.lang.Integer">
        <column name="user_id" />
        <generator class="identity" />
    </id>
    <many-to-one name="groupe" class="com.beans.Groupe" fetch="select">
        <column name="groupe_id" not-null="true" />
    </many-to-one>
    <property name="username" type="string">
        <column name="username" length="45" not-null="true" />
    </property>
    <property name="password" type="string">
        <column name="password" length="45" not-null="true" />
    </property>
    <property name="email" type="string">
        <column name="email" length="45" not-null="true" />

    </property>

</class>
 </hibernate-mapping>

groupe.java:

package com.beans;

// default package
// Generated 3 janv. 2013 12:07:19 by Hibernate Tools 3.4.0.CR1

import java.util.HashSet;
import java.util.Set;

/**
 * Groupe generated by hbm2java
 */
 public class Groupe implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private Integer groupeId;
private String groupeName;
private String groupeRole;
private Set users = new HashSet(0);

public Groupe() {
}

public Groupe(String groupeName, String groupeRole) {
    this.groupeName = groupeName;
    this.groupeRole = groupeRole;
}

public Groupe(String groupeName, String groupeRole, Set users) {
    this.groupeName = groupeName;
    this.groupeRole = groupeRole;
    this.users = users;
}

public Integer getGroupeId() {
    return this.groupeId;
}

public void setGroupeId(Integer groupeId) {
    this.groupeId = groupeId;
}

public String getGroupeName() {
    return this.groupeName;
}

public void setGroupeName(String groupeName) {
    this.groupeName = groupeName;
}

public String getGroupeRole() {
    return this.groupeRole;
}

public void setGroupeRole(String groupeRole) {
    this.groupeRole = groupeRole;
}

public Set getUsers() {
    return this.users;
}

public void setUsers(Set users) {
    this.users = users;
}

}

Groupe.hbm.xml

<?xml version="1.0"?>
 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 <!-- Generated 3 janv. 2013 12:07:19 by Hibernate Tools 3.4.0.CR1 -->
 <hibernate-mapping>
   <class name="Groupe" table="groupe" catalog="apurement">
      <id name="groupeId" type="java.lang.Integer">
        <column name="groupe_id" />
        <generator class="identity" />
      </id>
     <property name="groupeName" type="string">
        <column name="groupe_name" length="100" not-null="true" />
    </property>
    <property name="groupeRole" type="string">
        <column name="groupe_role" length="45" not-null="true" />
    </property>
    <set name="users" table="user" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="groupe_id" not-null="true" />
        </key>
        <one-to-many class="com.beans.User" />
    </set>
  </class>
 </hibernate-mapping>

hibernate.cfg:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                     "http://hibernate.sourceforge.net/hibernate- configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory name="sf">
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost/apurement</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <mapping class="com.beans.User" resource="com/beans/User.hbm.xml"/>
  <mapping class="com.beans.Groupe" resource="com/beans/Groupe.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

我不知道发生了什么。

最佳答案

您有一个用户,该用户具有到组的多对一映射,并且非空指定为 true。您正在尝试保留(插入)没有 Groupe 的用户。

关于java - Hibernate和mysql错误: Cannot add or update a child row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14141175/

相关文章:

java - JAXB 解析带有变量的 XML 文件(例如 $(var1) )

php - 尝试对单个 mysql 查询中的数据进行排序和显示

php - 使用 $_POST 将选定的项目值发送到服务器

Spark/scala 中的 SQL 查询大小超过 Integer.MAX_VALUE

sql - 存储过程获取参数列表和当前值

java - JTable 和外观

java - 在 Java 中针对接口(interface)进行编程与在 C/C++ 中使用头文件的概念相同吗?

java - 如何在 Windows 10 的 Java Swing JButton 中获得白色背景

php - 将 Mysql 附近查询转换为 Laravel API 查询

sql - 根据日期范围汇总每月的天数