gpt4 book ai didi

java - Hibernate 级联保存错误(无法添加或更新子行 : a foreign key constraint fails)

转载 作者:行者123 更新时间:2023-11-29 03:27:56 26 4
gpt4 key购买 nike

我是 hibernate 的新手,在学习了一些教程之后,我尝试自己制作一个使用 hibernate 存储持久数据的示例。使用:MySQL 5.7 和 Hibernate 5.0.3 以及 Eclipse IDE。

我寻找了类似的主题,但没有运行解决方案。

我的 Java 类是:

Dossier.java (Folder containing multiple files)

package com.testhibernate;
import java.util.ArrayList;
import java.util.List;

public class Dossier {

private Long id;
private String name;
private List<Fichier> files;

public Dossier()
{
setName("Folder");
files = new ArrayList<Fichier>();
}

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public List<Fichier> getFiles()
{
return files;
}

public void addFile(Fichier a_file)
{
a_file.setFolder(this);
files.add(a_file);
}
}

Fichier.java (File contained in a Folder) package com.testhibernate;

public class Fichier {
private Long id;
private String name;
private Dossier folder;

public Fichier()
{
setName("Fichier");
}

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public void setFolder(Dossier a_folder) {
folder = a_folder;
}

public Dossier getFolder() {
return (folder);
}
}

我还创建了一个类来处理保存请求

ManageDB.java (Contains starting main function)

package com.testhibernate.dbaccess;

import java.util.List;
import java.util.Iterator;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import com.testhibernate.Dossier;
import com.testhibernate.Fichier;


public class ManageDB {

private static SessionFactory factory;
private static boolean configured = false;

private static void log(String a_str)
{
System.out.println("[[MANAGEDB@: " + a_str + "]");
}

/**
* @name setup
* @brief configure the factory session
*/
public static void setup()
{
log("Setup");
ServiceRegistry serviceRegistry;
if (!configured)
{
try{
Configuration configuration = new Configuration();
configuration.configure();
configuration.addClass(Dossier.class);
configuration.addClass(Fichier.class);

serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
factory = configuration.buildSessionFactory(serviceRegistry);
configured = true;
}catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
}
}

/**
* @name addFolder.
* @param a_folder the folder to save
* @return ID of the added call.
*/
public static Long addFolder(Dossier a_folder)
{
log("addFolder");
Session session = factory.openSession();
Transaction tx = null;
Long folderID = null;
try {
tx = session.beginTransaction();
folderID = (Long) session.save(a_folder);
tx.commit();
}catch(HibernateException e)
{
if (tx != null)
tx.rollback();
log("Error");
e.printStackTrace();
}finally
{
session.close();
}

return(folderID);
}

public static Long addFile(Long folderID, Fichier a_file)
{
log("addFile");
Session session = factory.openSession();
//Load Dossier persistent object
Dossier l_folder = (Dossier) session.load(Dossier.class, folderID);
log("Folder@Name: " + l_folder.getName());
log("Folder@FilesCount: " + l_folder.getFiles().size());
//add the file
l_folder.addFile(a_file);
log("Folder@FilesCount: " + l_folder.getFiles().size());
session.flush();
return(a_file.getId());
}

public static void main(String arg[])
{
//Instantiate Dossier (a folder)
Dossier folder = new Dossier();
//Instantiate 4 Fichier objects (4 files)
Fichier file1, file2, file3, file4;
file1 = new Fichier();
file2 = new Fichier();
file3 = new Fichier();
file4 = new Fichier();
file1.setName("File1");
file2.setName("File2");
file3.setName("File3");
file4.setName("File4");

//Add files to the folder
folder.addFile(file1);
folder.addFile(file2);
folder.addFile(file3);

//Configure session factory
ManageDB.setup();
//Add folder to database
ManageDB.addFolder(folder);
}
}

Hibernate 文件(我使用带有 XML 的 Hibernate,没有注释)。

hibernate.cfg.xml

<?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="HibernateTestSessionFactory">
<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/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
</session-factory>
</hibernate-configuration>

Dossier.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 20 nov. 2015 03:16:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.testhibernate.Dossier" table="DOSSIER">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<list name="files" table="FICHIER" access="field" cascade="all">
<key>
<column name="ID" />
</key>
<list-index></list-index>
<one-to-many class="com.testhibernate.Fichier" />
</list>
</class>
</hibernate-mapping>

Fichier.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 20 nov. 2015 03:16:28 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.testhibernate.Fichier" table="FICHIER">
<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<many-to-one name="folder" class="com.testhibernate.Dossier" fetch="join">
<column name="FOLDER" />
</many-to-one>
</class>
</hibernate-mapping>

当我执行程序时发生错误:

ERROR Cannot add or update a child row: a foreign key constraint fails (test.fichier, CONSTRAINT FKa0k27gcqnhentsb26vm3cwki0 FOREIGN KEY (ID) REFERENCES dossier (ID))

当"file"列表中只有一个元素时,一切正常。

Database model generated automatically

DOSSIER Table

+---------+-------------------------------------------------------
| Table | Create Table
+---------+-------------------------------------------------------
| dossier | CREATE TABLE `dossier` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`NAME` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
+---------+-------------------------------------------------------
1 row in set (0.00 sec)

FICHIER Table

-----------------------------------------------------------------------------------------+
| Table | Create Table

|
+---------+-------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------+
| fichier | CREATE TABLE `fichier` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`NAME` varchar(255) DEFAULT NULL,
`FOLDER` bigint(20) DEFAULT NULL,
`files_ORDER` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `FKgc8774xohnq2sadsw2bum3kn7` (`FOLDER`),
CONSTRAINT `FKa0k27gcqnhentsb26vm3cwki0` FOREIGN KEY (`ID`) REFERENCES `dossier` (`ID`),
CONSTRAINT `FKgc8774xohnq2sadsw2bum3kn7` FOREIGN KEY (`FOLDER`) REFERENCES `dossier` (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
+---------+-------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------+
1 row in set (0.04 sec)

谢谢!

最佳答案

您要在文件夹之前添加文件。但是 File 表有一个指向 Folder 的外键:它还不存在。

只需在保存文件之前保存您的文件夹。

这不是 Hibernate 问题,这是数据库问题。

关于java - Hibernate 级联保存错误(无法添加或更新子行 : a foreign key constraint fails),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33832937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com