Home » Hibernate-3.3.2.GA » org.hibernate » intercept » [javadoc | source]

    1   /*
    2    * Hibernate, Relational Persistence for Idiomatic Java
    3    *
    4    * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
    5    * indicated by the @author tags or express copyright attribution
    6    * statements applied by the authors.  All third-party contributions are
    7    * distributed under license by Red Hat Middleware LLC.
    8    *
    9    * This copyrighted material is made available to anyone wishing to use, modify,
   10    * copy, or redistribute it subject to the terms and conditions of the GNU
   11    * Lesser General Public License, as published by the Free Software Foundation.
   12    *
   13    * This program is distributed in the hope that it will be useful,
   14    * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   15    * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
   16    * for more details.
   17    *
   18    * You should have received a copy of the GNU Lesser General Public License
   19    * along with this distribution; if not, write to:
   20    * Free Software Foundation, Inc.
   21    * 51 Franklin Street, Fifth Floor
   22    * Boston, MA  02110-1301  USA
   23    *
   24    */
   25   package org.hibernate.intercept;
   26   
   27   import org.hibernate.engine.SessionImplementor;
   28   import org.hibernate.LazyInitializationException;
   29   
   30   import java.util.Set;
   31   import java.io.Serializable;
   32   
   33   /**
   34    * @author Steve Ebersole
   35    */
   36   public abstract class AbstractFieldInterceptor implements FieldInterceptor, Serializable {
   37   
   38   	private transient SessionImplementor session;
   39   	private Set uninitializedFields;
   40   	private final String entityName;
   41   
   42   	private transient boolean initializing;
   43   	private boolean dirty;
   44   
   45   	protected AbstractFieldInterceptor(SessionImplementor session, Set uninitializedFields, String entityName) {
   46   		this.session = session;
   47   		this.uninitializedFields = uninitializedFields;
   48   		this.entityName = entityName;
   49   	}
   50   
   51   
   52   	// FieldInterceptor impl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   53   
   54   	public final void setSession(SessionImplementor session) {
   55   		this.session = session;
   56   	}
   57   
   58   	public final boolean isInitialized() {
   59   		return uninitializedFields == null || uninitializedFields.size() == 0;
   60   	}
   61   
   62   	public final boolean isInitialized(String field) {
   63   		return uninitializedFields == null || !uninitializedFields.contains( field );
   64   	}
   65   
   66   	public final void dirty() {
   67   		dirty = true;
   68   	}
   69   
   70   	public final boolean isDirty() {
   71   		return dirty;
   72   	}
   73   
   74   	public final void clearDirty() {
   75   		dirty = false;
   76   	}
   77   
   78   
   79   	// subclass accesses ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   80   
   81   	protected final Object intercept(Object target, String fieldName, Object value) {
   82   		if ( initializing ) {
   83   			return value;
   84   		}
   85   
   86   		if ( uninitializedFields != null && uninitializedFields.contains( fieldName ) ) {
   87   			if ( session == null ) {
   88   				throw new LazyInitializationException( "entity with lazy properties is not associated with a session" );
   89   			}
   90   			else if ( !session.isOpen() || !session.isConnected() ) {
   91   				throw new LazyInitializationException( "session is not connected" );
   92   			}
   93   
   94   			final Object result;
   95   			initializing = true;
   96   			try {
   97   				result = ( ( LazyPropertyInitializer ) session.getFactory()
   98   						.getEntityPersister( entityName ) )
   99   						.initializeLazyProperty( fieldName, target, session );
  100   			}
  101   			finally {
  102   				initializing = false;
  103   			}
  104   			uninitializedFields = null; //let's assume that there is only one lazy fetch group, for now!
  105   			return result;
  106   		}
  107   		else {
  108   			return value;
  109   		}
  110   	}
  111   
  112   	public final SessionImplementor getSession() {
  113   		return session;
  114   	}
  115   
  116   	public final Set getUninitializedFields() {
  117   		return uninitializedFields;
  118   	}
  119   
  120   	public final String getEntityName() {
  121   		return entityName;
  122   	}
  123   
  124   	public final boolean isInitializing() {
  125   		return initializing;
  126   	}
  127   }

Home » Hibernate-3.3.2.GA » org.hibernate » intercept » [javadoc | source]