-->
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: find state of entity
PostPosted: Tue Jan 27, 2009 1:34 am 
Newbie

Joined: Wed Dec 17, 2008 12:17 pm
Posts: 11
is there a way to find the state of entity ie is it in detached or PERSISTENT state ?to avoid lazy loading exception.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 2:46 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
You can use session.contains to check if the object is associated to the current session.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 5:30 pm 
Newbie

Joined: Wed Dec 17, 2008 12:17 pm
Posts: 11
mmerder wrote:
You can use session.contains to check if the object is associated to the current session.


here is the code from hibernate which determines the state of entity

and how good is using session.contains() compared to this ?



Code:
   protected int getEntityState(
         Object entity,
         String entityName,
         EntityEntry entry, //pass this as an argument only to avoid double looking
         SessionImplementor source) {

      if ( entry != null ) { // the object is persistent

         //the entity is associated with the session, so check its status
         if ( entry.getStatus() != Status.DELETED ) {
            // do nothing for persistent instances
            if ( log.isTraceEnabled() ) {
               log.trace(
                     "persistent instance of: " +
                           getLoggableName( entityName, entity )
               );
            }
            return PERSISTENT;
         }
         else {
            //ie. e.status==DELETED
            if ( log.isTraceEnabled() ) {
               log.trace(
                     "deleted instance of: " +
                           getLoggableName( entityName, entity )
               );
            }
            return DELETED;
         }

      }
      else { // the object is transient or detached

         //the entity is not associated with the session, so
         //try interceptor and unsaved-value

         if ( ForeignKeys.isTransient( entityName, entity, getAssumedUnsaved(), source ) ) {
            if ( log.isTraceEnabled() ) {
               log.trace(
                     "transient instance of: " +
                           getLoggableName( entityName, entity )
               );
            }
            return TRANSIENT;
         }
         else {
            if ( log.isTraceEnabled() ) {
               log.trace(
                     "detached instance of: " +
                           getLoggableName( entityName, entity )
               );
            }
            return DETACHED;
         }

      }
   }

   public static boolean isTransient(String entityName, Object entity, Boolean assumed, SessionImplementor session)
   throws HibernateException {
      
      if (entity==LazyPropertyInitializer.UNFETCHED_PROPERTY) {
         // an unfetched association can only point to
         // an entity that already exists in the db
         return false;
      }
      
      // let the interceptor inspect the instance to decide
      Boolean isUnsaved = session.getInterceptor().isTransient(entity);
      if (isUnsaved!=null) return isUnsaved.booleanValue();
      
      // let the persister inspect the instance to decide
      EntityPersister persister = session.getEntityPersister(entityName, entity);
      isUnsaved = persister.isTransient(entity, session);
      if (isUnsaved!=null) return isUnsaved.booleanValue();

      // we use the assumed value, if there is one, to avoid hitting
      // the database
      if (assumed!=null) return assumed.booleanValue();
      
      // hit the database, after checking the session cache for a snapshot
      Object[] snapshot = session.getPersistenceContext()
              .getDatabaseSnapshot( persister.getIdentifier( entity, session.getEntityMode() ), persister );
      return snapshot==null;

   }



Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 4:37 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
To me it seems rather complicated just to avoid a lazy-loading-exception.

Have you taken a look at the session.contains-implementation?

Wouldn't it be easier at all to catch the LazyInitializationException ?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 11:20 am 
Newbie

Joined: Wed Dec 17, 2008 12:17 pm
Posts: 11
mmerder wrote:
To me it seems rather complicated just to avoid a lazy-loading-exception.

Have you taken a look at the session.contains-implementation?

Wouldn't it be easier at all to catch the LazyInitializationException ?


If i have to catch LazyInitializationException I have to do it in several places , better approach to me is somehow tell hibernate to reattach entity and retrieve the collection , is there any listener which i can override to do this ?


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.