Hi at all,
i have there an "many-to-many" mapping:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="models.Page" table="Page">
<id column="id" name="Id">
<generator class="increment"/>
</id>
<property column="Name" name="Name"/>
<many-to-one class="models.Layout" column="layout_id" name="Layout"/>
<set name="Content" table="Pagecontent_content">
<key column="page_id"/>
<many-to-many class="models.Content" column="content_id"/>
</set>
<set name="Title" table="Pagetitle_content">
<key column="page_id"/>
<many-to-many class="models.Content" column="content_id"/>
</set>
<set name="SubMenu" table="Page_submenuitem">
<key column="page_id"/>
<many-to-many class="models.SubMenuItem" column="submenuitem_id" not-found="ignore"/>
</set>
</class>
</hibernate-mapping>
I want to save an object with two "contents" in "Title" and in "Content". That works just half. It runs with out exceptions, but it saves just one of the entry in the list. Are two or more entry's in the list, it save's all entry's to the content table but does not save the relations into the relation tables for page.
The saving code:
Code:
fSessionFactory = new Configuration().configure().buildSessionFactory();
fSession = fSessionFactory.openSession();
Transaction tx = fSession.beginTransaction();
fSession.saveOrUpdate(page.Layout);
for(int i = 0; page.Content != null && i < page.Content.size(); i++) {
fSession.saveOrUpdate(page.Content.get(i));
}
for(int i = 0; page.Title != null && i < page.Title.size(); i++) {
fSession.saveOrUpdate(page.Title.get(i));
}
for(int i = 0; page.SubMenu != null && i < page.SubMenu.size(); i++) {
for(int j = 0; page.SubMenu.get(i).Content != null && j < page.SubMenu.get(i).Content.size(); j++) {
fSession.saveOrUpdate(page.SubMenu.get(i).Content.get(j));
}
fSession.saveOrUpdate(page.SubMenu.get(i));
}
fSession.saveOrUpdate(page);
tx.commit();
Is someone able to help or knows what the problem is?