0

だから私はテストアプリケーションを書いています。収集したすべてのクラス内にコレクションを保存したい。収集された各クラスには、データの一意のリストがあります。データクラスを保存できません。

私が得ているエラー ERROR: 参照整合性制約違反: "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