-->
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.  [ 8 posts ] 
Author Message
 Post subject: Cannot swap two elements in a list
PostPosted: Thu Nov 27, 2003 2:01 pm 
Beginner
Beginner

Joined: Wed Oct 08, 2003 6:34 am
Posts: 29
Hi there,

I would like to swap two elements in a list, but I'm not able to do it. Here is my code:
Code:
    Session hibernateSession = sessionFactory.openSession(  );
    Transaction transaction = hibernateSession.beginTransaction(  );

    try
      {
      String categoryId = httpServletRequest.getParameter( "categoryId" );
      Category category = (Category) hibernateSession.load( Category.class, categoryId );
      Publication publication = category.getPublication(  );

      int categoryIndex = publication.getCategories(  ).indexOf( category );
      if( categoryIndex > 0 )
        {
        Category swappedCategory = (Category) publication.getCategories(  ).remove( categoryIndex - 1 );
        publication.getCategories(  ).add( categoryIndex, swappedCategory );
        }

      hibernateSession.update( publication );
      transaction.commit(  );
      }
    finally
      {
      hibernateSession.close(  );
      }



Here is my mapping:
Code:
        <list name="categories" lazy="false" inverse="true"       cascade="all">
              <key column="publication"/>
              <index column="rank" type="integer" length="10"/>
              <one-to-many class="com.actaris.news.domain.Category"/>
        </list>
............
    <class name="com.actaris.news.domain.Category" table="category" dynamic-update="false" dynamic-insert="false">
        <many-to-one name="publication" class="com.actaris.news.domain.Publication" cascade="none" outer-join="auto" update="true" insert="true" column="publication" not-null="true" unique="false"/>



Am I doing anything stupid or what?

Regards
Jose


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 5:54 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hibernate does not support bidirectional relationship with indexed collection (such as list). The many-to-one is irrevelent.
Thus, you must set inverse="false" in the <list/>.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 6:39 pm 
Beginner
Beginner

Joined: Wed Oct 08, 2003 6:34 am
Posts: 29
If I set inverse to false I get exceptions when deleting entities from the collection (foreign key not null). Should I set not-null to false in the foreign key and then set inverse to false?

Regards
Jose


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 7:09 pm 
Beginner
Beginner

Joined: Wed Oct 08, 2003 6:34 am
Posts: 29
Sorry, I'm new to hibernate and I think I haven't understood bidirectional associations.

What makes a bidirectional association? Do you have a bidirectional association when you have two entities that have a reference to each other? Or do you need to mark one end of the association with inverse="true" to consider this association bidirectional? What does imply a bidirectional association? Does one end of the association get updated automatically (the one with inverse="true") when the other end is changed?

Thanks in advance, regards
Jose


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 27, 2003 7:16 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Yes, an association where you can navigate from one entity to the other, starting on either side. In this case, you have to mark one end as inverse, see here for an explanation:

http://www.hibernate.org/Documentation/ ... nverseTrue

Hibernate supports only Sets or Bags for the "many" side of a one-to-many bidirectional association, not indexed collections like List or Array. A good start is the "Parent/Child Relationship" chapter in the reference documentation.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 5:04 am 
Newbie

Joined: Thu Aug 28, 2003 8:41 pm
Posts: 17
I think hibernate supports List for the "many" side of a one-to-many bidirectional association,you can maintain the index column in the "many" side(map the index column as a property of the "many" class),when you add a "many" object to an "one" object,set the index property of the "many" object manually.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 28, 2003 8:20 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
yehs220 wrote:
I think hibernate supports List for the "many" side of a one-to-many bidirectional association,you can maintain the index column in the "many" side(map the index column as a property of the "many" class),when you add a "many" object to an "one" object,set the index property of the "many" object manually.

Is that what you call a supported feature ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 30, 2003 9:47 pm 
Newbie

Joined: Thu Aug 28, 2003 8:41 pm
Posts: 17
At least it does work,and it is more efficient,it issues only one insert to add a "many" object.


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