0

所以我写了一个测试应用程序。我想在每个收集的类中保存集合。每个收集到的类都有一个唯一的数据列表。我无法保存数据类。

我收到的错误错误:违反参照完整性约束:“FK2EEFAAF2485486:PUBLIC.DATA FOREIGN KEY(NUMBER) REFERENCES PUBLIC.COLLECTED(KEY) (4)”;SQL 语句:插入数据(数字,数据)值(null,?)[23506-170]

主班

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernatecollectiontest;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;

/**
 *
 * @author ivan
 */
public class HibernateCollectionTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        Session session = Util.getSessionFactory().openSession();
        Collected temp = new Collected();
        temp.setDescription("test");
        List<Data> local= new ArrayList<>(); 
        for(int i=0;i<5;i++)
        {
            Data test = new Data();
            test.setData("Data"+i);
            local.add(test);
        }
        temp.setDatas(local);
        session.beginTransaction();
        session.save(temp);
        session.getTransaction().commit();
        Criteria crit = session.createCriteria(Collected.class);
        List<Collected> dump =crit.list();
        for(int i = 0 ; i < dump.size();i++)
        {
            System.out.println(dump.get(i).getKey()+"  key "+dump.get(i).getDescription() +"  describe ");
            System.out.println("Size of collection is "+dump.get(i).getDatas().size());
            for(int n=0;n<dump.get(i).getDatas().size();n++)
            {
                System.out.println(dump.get(i).getDatas().get(n).getData());
            }
        }
        //session.flush();
    }
}

持久类:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernatecollectiontest;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ivan
 */
public class Collected 
{
    private String description;
    private int key;
    private List<Data> datas = new ArrayList<>();

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the key
     */
    public int getKey() {
        return key;
    }

    /**
     * @param key the key to set
     */
    public void setKey(int key) {
        this.key = key;
    }

    /**
     * @return the datas
     */
    public List<Data> getDatas() {
        return datas;
    }

    /**
     * @param datas the datas to set
     */
    public void setDatas(List<Data> datas) {
        this.datas = datas;
    }

}

集合内的数据类:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernatecollectiontest;

/**
 *
 * @author ivan
 */
public class Data 
{
    private int number;
    private String data;

    /**
     * @return the number
     */
    public int getNumber() {
        return number;
    }

    /**
     * @param number the number to set
     */
    public void setNumber(int number) {
        this.number = number;
    }

    /**
     * @return the data
     */
    public String getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(String data) {
        this.data = data;
    }
}

休眠映射:

    <!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">
jdbc:h2:testdatabase
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="Collected.hbm.xml"/>
<mapping resource="Data.hbm.xml"/>
</session-factory>
</hibernate-configuration>

数据.hbm

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernatecollectiontest">
<class name="Data" table="data">
<id name="number" column="number" type="java.lang.Integer">
<generator class="native"/>
</id>
<property column="data" name="data" type="java.lang.String"/>
</class>
</hibernate-mapping>

采集到.hbm

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernatecollectiontest">
<class name="Collected" table="collected">
<id name="key" column="key" type="java.lang.Integer">
<generator class="native"/>
</id>
<property column="description" name="description" type="java.lang.String"/>
<list name="datas" table="data" lazy="false" cascade="all">
<key column="number"/>
<list-index column="sortOrder"/>
<one-to-many class="hibernatecollectiontest.Data"/>
</list>
</class>
</hibernate-mapping>
4

1 回答 1

0

首先,

交易未提交。,

 //session.getTransaction().commit();

上面的行在代码库中被注释,

于 2013-04-23T21:08:47.513 回答