-->
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.  [ 3 posts ] 
Author Message
 Post subject: Indexes not getting updated (I'm sooo close.)
PostPosted: Thu Jan 05, 2006 4:23 pm 
Newbie

Joined: Thu Jan 05, 2006 4:13 pm
Posts: 4
I feel like I'm sooo close and just doing something dumb.

I've got an Activity that contains a list of Contributions. The Contributions point back to a single activity as their parent. I'm trying to let Hibernate maintain the indexes for me. When I add a list of contributions for the very first time, it works correctly, with the indexes on the contributions being what I expect. contrib1[0], contrib2[1], contrib3[2]

However, if I try to add a new contribution to the list at the beginning, only the contribution I'm adding gets updated. The others don't get reindexed.

It becomes contrib4[0], contrib1[0], contrib2[1], contrib3[2].

I've tried to "copy all elements, clear() the original instance, and addAll()" to no avail. From this article: http://forum.hibernate.org/viewtopic.php?t=945944

I've gotta be close, does anyone see anything I might be missing?

Here's my list of Contribs in my Activity.hbm.xml

Code:
<list name="contribs"
     lazy="true"
     cascade="all-delete-orphan">
   <key column="activityID"
        not-null="true"
        update="false"/>
   <list-index column="pos" base="0"/>
   <one-to-many class="Contrib"/>
</list>


Here's my activity from the contribution.

Code:
<many-to-one name="activity" class="Activity" column="activityID" not-null="true" insert="false" update="false" />

Here's where I add a new Contribution:
Code:
public void addContrib(Contrib inContrib, int pos)
{
   inContrib.setActivity(this);
   ArrayList newArray = new ArrayList(contribs);
   newArray.add(pos,inContrib);
   contribs.clear();
   contribs.addAll(newArray);
}


Thanks for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 06, 2006 8:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Probably this will help: http://www.hibernate.org/193.html


Top
 Profile  
 
 Post subject: Found the solution
PostPosted: Fri Jan 06, 2006 11:36 pm 
Newbie

Joined: Thu Jan 05, 2006 4:13 pm
Posts: 4
Yup. I was right. It turned out to be pretty easy.

I had update="false" instead of update="true".

It should be:
Code:
<list name="contribs"
       lazy="true"
       cascade="all-delete-orphan">
       <key column="activityID"
           not-null="true"
           update="true"/>
   <list-index column="pos"/>
   <one-to-many class="Contrib"/>
</list>


And the code to put the object into the right spot was...

Code:
public void addContrib2(Contrib inContrib, int pos)
throws GSException
{
  if(inContrib == null)
{
      throw new IllegalArgumentException("Null Contribution cannot be added");
}
      
if(inContrib.getActivity() != null)
{
   inContrib.getActivity().getContribs().remove(inContrib);
}
      
if (pos < 0)
{
   pos = 0;
}
else if(pos > contribs.size())
{
        pos = contribs.size();
}

List lowerList = new ArrayList<Contrib>(this.getContribs().subList(0,pos));
List upperList = new ArrayList<Contrib>(this.getContribs().subList(pos,  getContribs().size()));
      
contribs.clear();
      
contribs.addAll(lowerList);
contribs.add(inContrib);
contribs.addAll(upperList);
}


I have noticed that, if you do a:

Code:
contribs.add(pos, Contrib);


The object in the position you are writing to will keep its same position. Thus, you'll get contrib1[0], contrib4[0], contrib2[1], contrib3[2] (I just wanted to use the word "thus".)

Some catch words in case people search for them: overwritten, reindexed, overwrite


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.