-->
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: Problem updating a collection
PostPosted: Tue Nov 23, 2004 11:26 am 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:49 pm
Posts: 33
Location: Bogota, Colombia
Hi, consider an object A that has a one-to-many collection of B objects. Usually when I set an entire collection to an object (A.setBs(set);) what it does is deleting all the items in the collection and adding the new ones, which generates a delete in sql that eliminates the records and then an insert that creates the new ones. The problem I'm having is that the deletion doesnt occur so, if my collection has objects B1, B2 and B3, and I eliminate B3, the result is B1, B2, B1, B2. (the new collection to be set has B1 and B2 and thus it duplicates them)

(I've done this same operation with other classes and it works just fine.. but I cant find any difference. Need I check my equals() and hashcode() in the B class?)

Has anyone been here before? thanks..

Hibernate version: 2.1.2

Mapping documents:
A:

Code:
    <class
        name="A"
        table="As"
        dynamic-update="false"
        dynamic-insert="false"
        select-before-update="false"
    >

    (...)

        <set
            name="Bs"
            lazy="false"
            inverse="true"
            cascade="save-update"
            sort="unsorted"
        >
            <cache
                usage="read-write"
             />

              <key
                  column="A_ID"
              >
              </key>

              <one-to-many
                  class="B"
              />

        </set>

    </class>

B:

Code:
    <class
        name="B"
        table="Bs"
        dynamic-update="false"
        dynamic-insert="false"
        select-before-update="false"
    >

        <many-to-one
            name="AObj"
            class="A"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            access="property"
            column="A_ID"
        />

    </class>


Code between sessionFactory.openSession() and session.close():
Code:
   public void updateBs(A item, HashSet set)
      throws HibernateException, BusinessException {

      if (item == null) log.debug("****** A IS NULL *******");
      log.debug("Updating Bs for A: " + item.getId());

      log.debug("Set size: " + set.size());

      item.setBs(set);
      HibernateUtil.getSession().update(item);
      log.debug("Done.");
   }



Full stack trace of any exception that occurs: No Exception. Just the insert without the previous delete.

Name and version of the database you are using: Oracle 9i


Top
 Profile  
 
 Post subject: An update
PostPosted: Tue Nov 23, 2004 12:17 pm 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:49 pm
Posts: 33
Location: Bogota, Colombia
An update..

Reading the log in detail, I found this: In my logic I first update the properties of the A object and then try to update its set of Bs before flushing. In the log I found that when I save the A object it cascades saveOrUpdate over the Bs collection:

Code:
2004-11-23 10:48:05,792 DEBUG [net.sf.hibernate.impl.SessionImpl] updating [com.edesa.matri.persistence.Inscripcion#44]
2004-11-23 10:48:05,823 DEBUG [net.sf.hibernate.engine.Cascades] processing cascades for: com.edesa.matri.persistence.Inscripcion
2004-11-23 10:48:05,823 DEBUG [net.sf.hibernate.engine.Cascades] cascading to collection: com.edesa.matri.persistence.Inscripcion.instituciones
2004-11-23 10:48:05,823 DEBUG [net.sf.hibernate.engine.Cascades] cascading to saveOrUpdate()
2004-11-23 10:48:05,823 DEBUG [net.sf.hibernate.engine.Cascades] id unsaved-value strategy NULL
2004-11-23 10:48:05,823 DEBUG [net.sf.hibernate.impl.SessionImpl] saveOrUpdate() previously saved instance with id: 80
2004-11-23 10:48:05,823 DEBUG [net.sf.hibernate.impl.SessionImpl] updating [com.edesa.matri.persistence.Opcion#80]
2004-11-23 10:48:05,823 DEBUG [net.sf.hibernate.engine.Cascades] cascading to saveOrUpdate()
2004-11-23 10:48:05,823 DEBUG [net.sf.hibernate.engine.Cascades] id unsaved-value strategy NULL
2004-11-23 10:48:05,823 DEBUG [net.sf.hibernate.impl.SessionImpl] saveOrUpdate() previously saved instance with id: 72
2004-11-23 10:48:05,823 DEBUG [net.sf.hibernate.impl.SessionImpl] updating [com.edesa.matri.persistence.Opcion#72]
2004-11-23 10:48:05,823 DEBUG [net.sf.hibernate.engine.Cascades] cascading to saveOrUpdate()

(...)

2004-11-23 10:48:05,823 DEBUG [net.sf.hibernate.engine.Cascades] done processing cascades for: com.edesa.matri.persistence.Inscripcion


So, when I try to set the new collection it says:

Code:
2004-11-23 10:48:06,026 DEBUG [net.sf.hibernate.impl.SessionImpl] object already associated with session


Then it inserts the new collection over the other. :S


Top
 Profile  
 
 Post subject: Monologue.
PostPosted: Tue Nov 23, 2004 1:34 pm 
Beginner
Beginner

Joined: Tue Sep 21, 2004 1:49 pm
Posts: 33
Location: Bogota, Colombia
Well I made it work but I dont think it's the best way to do it, I'd appreciate some advise on it..

I changed my code to this:

Code:
public void updateBs(A item, HashSet set)
   throws HibernateException, BusinessException {

   if (item == null) log.debug("****** A IS NULL *******");
   log.debug("Updating Bs for A: " + item.getId());
   log.debug("Number of Bs: " + set.size());

   // Eliminates current Bs from A.
   item.setBs(null);

   // Eliminates A's Bs from database.
   String q = "from B b " +
      "where b.a.id = " + item.getId();
   HibernateUtil.getSession().delete(q);

   // Adds the new Bs.
   item.setBs(set);
   HibernateUtil.getSession().update(item);
   log.debug("Done.");
}


I dont think it looks so healthy.. is there a way to make it simpler?


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.