因此,我寫一個測試應用程序。我想在每個收集的類中保存集合。每個收集的類都有一個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>
沒有告訴我們你得到了什麼錯誤,我看不出有人能真正幫助。然而,你沒有關閉會議的事實非常令人懷疑。 – 2013-04-23 20:43:19
主鍵約束衝突是沖洗時我得到的錯誤。 – user1633277 2013-04-23 21:26:47
然後你的問題是你正在嘗試保存一個重複主鍵的對象,並且你需要修復它。 – 2013-04-24 15:19:13