2013-04-23 163 views
0

因此,我写一个测试应用程序。我想在每个收集的类中保存集合。每个收集的类都有一个unieq数据列表。我不能保存数据类。休眠保存集合

我得到的错误 错误:参照完整性约束违规:“FK2EEFAAF2485486:PUBLIC.DATA FOREIGN KEY(NUMBER)REFERENCES PUBLIC.COLLECTED(KEY)(4)”; SQL语句:插入到数据(数字数据)值(?空)[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; 
    } 
} 

的Hibernate映射:

<!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> 

Data.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> 

Collected.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> 
+0

没有告诉我们你得到了什么错误,我看不出有人能真正帮助。然而,你没有关闭会议的事实非常令人怀疑。 – 2013-04-23 20:43:19

+0

主键约束冲突是冲洗时我得到的错误。 – user1633277 2013-04-23 21:26:47

+0

然后你的问题是你正在尝试保存一个重复主键的对象,并且你需要修复它。 – 2013-04-24 15:19:13

回答

0

首先,

交易是没有得到承诺,

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

李上方ne在代码库中有所评论。,

+0

取消注释给出了一个错误,通过将所有级联添加到收集的hmb列表标记来解决该错误。然后我得到一个关于主键的错误。 – user1633277 2013-04-23 21:20:29

+0

你可以改变生成器类为“身份”,而不是“本地”,并追加你的实际错误的线程将有助于。 – 2013-04-23 21:34:01