-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: ManytoMany SaveOrUpdate saves just last entry (Java)
PostPosted: Mon Sep 27, 2010 1:42 pm 
Newbie

Joined: Mon Sep 27, 2010 1:20 pm
Posts: 2
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.