-->
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.  [ 5 posts ] 
Author Message
 Post subject: Mapping Collections
PostPosted: Tue Oct 26, 2004 5:03 pm 
Beginner
Beginner

Joined: Tue Oct 26, 2004 4:59 pm
Posts: 23
I don't know if this is how Hibernate is supposed to work or I am doing something wrong.

I am mapping a collection as a BAG in a many-to-many relation. Whenever the java List is structurally changed like an element is added or removed, when I do a saveOrUpdate Hibernate deletes all records corresponding to the List and then inserts new records. I was hoping that any elements that remained in the List would not be deleted and reinserted. Is this how it is supposed to work or am I doing something wrong?

Thanks,
Karl


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 26, 2004 6:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You are replacing the collection Hibernate set with one you created yourself somewhere?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 27, 2004 9:14 am 
Beginner
Beginner

Joined: Tue Oct 26, 2004 4:59 pm
Posts: 23
Here's the code. Essentially I have two lists, one from the persisted object, and one containing "new" values contained in the pageNo array. All of my operations are performed directly on the object returned by Hibernate.

Code:
            session = HibernateSessionFactory.getSessionFactory().openSession(ConnectionProperties.getConn("iwsdb"));
            newGroup = (Group) session.load(Group.class, groupNo);
            oldList = newGroup.getGroupPages();
            for(int i=0;i<pageNo.length;i++) {
               page_no = new Long(pageNo[i]);
               page = (Page) session.load(Page.class, page_no);
               newPageList.add(page);
            }
            List tempOldList = new ArrayList(oldPageList);
            for(int i=0;i<tempOldList.size();i++) {
               page = (Page) tempOldList.get(i);
               if(newPageList.contains(page)) {
                  newPageList.remove(page);
               } else {
                  newGroup.getGroupPages().remove(page);
               }
            }
            for(int i=0;i<newPageList.size();i++) {
               page = (Page) newPageList.get(i);
               newGroup.getGroupPages().add(page);
            }
           
            session.saveOrUpdate(newGroup);
            session.flush();
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 27, 2004 9:15 am 
Beginner
Beginner

Joined: Tue Oct 26, 2004 4:59 pm
Posts: 23
And here is my Hibernate mapping for the Group object.

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
   
   <class name="com.iws.model.group.Group" table="GROUPS">
      <id name="groupNo" column="GROUPS_NO" type="long" unsaved-value="0">
         <generator class="identity"/>
      </id>
      
      <property name="groupName"
              column="GROUPS_NAME"
              not-null="true"
              type="string" />
      
      <property name="groupDescription"
              column="GROUPS_DESC"
              not-null="true"
              type="string" />
      <bag name="groupPages" table="PAGEGROUPS" >
         <key column="GROUPS_NO" />
         <many-to-many class="com.iws.model.page.Page" column="PAGES_NO" outer-join="true"/>
      </bag>             
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 28, 2004 11:15 am 
Beginner
Beginner

Joined: Tue Oct 26, 2004 4:59 pm
Posts: 23
Bump.

No one can even tell me that this is supposed to work like this, or I'm doing something wrong?

Karl


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

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.