-->
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: reattaching objects. Can I avoid database access ?
PostPosted: Thu Jun 17, 2004 1:33 pm 
Regular
Regular

Joined: Mon Jun 14, 2004 1:42 pm
Posts: 80
Location: Brazil
Hi. I'm creating a new Object that references 3 other objects detached.
When I try to insert the new object , I get an error like that
object references an unsaved transient instance
So I learned that I have to reattach the objects with the "load" command.
Something like this:

Working example

Code:
Person person = new Person();
//...set props
// professor is one detached Job
person.setJob(session.load(professor,professor.getId()));
session.saveOrUpdate(person);
...
session.flush();

not working

Code:
Person person = new Person();
//...set props
// professor is one detached Job
person.setJob(professor);
session.saveOrUpdate(person);
...
session.flush();


Well, correct me if I'm wrong. The first example will go to the database to reload the Job Professor so I could create a new Person (or go to some cache maybe)
But in this case I don't want to reload the professor, since I have it already. I just want to set IdJob on the table Person. Is there a better way to do that (more performatic, elegant, or so) ? Some kind of flag like "don't mind in update me" to prevent the error.
In this case I mapped Person as a Set in job. But I'm open to other kind of experience in mapping :)

Cheers.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 17, 2004 3:42 pm 
Newbie

Joined: Thu Apr 08, 2004 10:24 am
Posts: 19
Location: Raleigh, NC
try locking professor with lock mode of NONE


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 18, 2004 12:55 pm 
Regular
Regular

Joined: Mon Jun 14, 2004 1:42 pm
Posts: 80
Location: Brazil
Excelent. Thank you for the valuable tip.
But now I'm getting another annoying problem. I constructed my Bean with a lazzy contructor for one collection, like:

Code:
Set getUniversities() {
  if (universities==null)
    universities = new HashSet(); 
  return universities
}


But I found that there is a check in the lock command that throws exception for any set that is not persistent class,even empty sets!:

reassociated object has dirty collection reference

class net.sf.hibernate.impl.OnLockVisitor
Code:
Object processCollection(Object collection, PersistentCollectionType type)
      throws HibernateException {
      
      SessionImpl session = getSession();
      CollectionPersister persister = session.getCollectionPersister( type.getRole() );
      
      if (collection==null) {
         //do nothing
      }
      else if ( collection instanceof PersistentCollection ) {
         PersistentCollection coll = (PersistentCollection) collection;

         if ( coll.setCurrentSession(session) ) {

            CollectionSnapshot snapshot = coll.getCollectionSnapshot();
            if ( SessionImpl.isOwnerUnchanged( snapshot, persister, getKey() ) ) {
               // a "detached" collection that originally belonged to the same entity
               if ( snapshot.getDirty() ) {
                  throw new HibernateException("reassociated object has dirty collection");
               }
               session.reattachCollection(coll, snapshot);
            }
            else {
               // a "detached" collection that belonged to a different entity
               throw new HibernateException("reassociated object has dirty collection reference");
            }

         }
         else {
            // a collection loaded in the current session
            // can not possibly be the collection belonging
            // to the entity passed to update()
            throw new HibernateException("reassociated object has dirty collection reference");
         }

      }
      else {
         // brand new collection
         //TODO: or an array!! we can't lock objects with arrays now??
         throw new HibernateException("reassociated object has dirty collection reference");
      }

      return null;

   }

The reference pdf says in Chapter 6. Collection Mapping:

Quote:
Collections obey the usual rules for value types: no shared references, created and deleted along with containing
entity. Due to the underlying relational model, they do not support null value semantics; Hibernate does not
distinguish between a null collection reference and an empty collection.

Well, in this case,DO Hibernate distinguish null x collection?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 21, 2004 9:22 am 
Regular
Regular

Joined: Mon Jun 14, 2004 1:42 pm
Posts: 80
Location: Brazil
Hello ? :)
well, I redesigned my classes removing any lazzy initialization of collections. Imagine if hibernate perform reads in all your properties, all collections will be initialized (ouch, process and memory will be used for nothing, and you end with strange lock errors!).
My conclusion : don't EVER think in code lazzy initialization on collections.
Better, set null collection if it`s not used on the transaction. Empty collections are for meaningfull collections.
Any comments ? Am I so wrong? :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 21, 2004 10:18 am 
Newbie

Joined: Thu Apr 08, 2004 10:24 am
Posts: 19
Location: Raleigh, NC
you are doing well if your performance problems are limited to the creation of a few extra HashSets. if you really must use lazy initialization, make a different method to do so.

e.g.

Code:
getUniversitySet()
{
   if (getUniversities() == null)
   {
      setUniversities(new HashSet());
   }
   return getUniversities();
}


--b


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.