📄 abstractentitypersister.java
字号:
//$Id: AbstractEntityPersister.java,v 1.33.2.58 2003/11/29 07:55:15 oneovthafew Exp $package net.sf.hibernate.persister;import java.io.Serializable;import java.lang.reflect.Constructor;import java.lang.reflect.Method;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.Map;import java.util.Set;import net.sf.cglib.proxy.Factory;import net.sf.cglib.beans.BulkBean;import net.sf.cglib.reflect.FastClass;import net.sf.hibernate.Hibernate;import net.sf.hibernate.HibernateException;import net.sf.hibernate.InstantiationException;import net.sf.hibernate.JDBCException;import net.sf.hibernate.Lifecycle;import net.sf.hibernate.MappingException;import net.sf.hibernate.PropertyAccessException;import net.sf.hibernate.PropertyNotFoundException;import net.sf.hibernate.StaleObjectStateException;import net.sf.hibernate.Validatable;import net.sf.hibernate.cache.CacheConcurrencyStrategy;import net.sf.hibernate.cfg.Environment;import net.sf.hibernate.dialect.Dialect;import net.sf.hibernate.engine.Cascades;import net.sf.hibernate.engine.Mapping;import net.sf.hibernate.engine.SessionFactoryImplementor;import net.sf.hibernate.engine.SessionImplementor;import net.sf.hibernate.engine.Versioning;import net.sf.hibernate.id.IdentifierGenerator;import net.sf.hibernate.id.IdentityGenerator;import net.sf.hibernate.impl.MessageHelper;import net.sf.hibernate.loader.BatchingEntityLoader;import net.sf.hibernate.loader.EntityLoader;import net.sf.hibernate.loader.Loader;import net.sf.hibernate.loader.UniqueEntityLoader;import net.sf.hibernate.mapping.Column;import net.sf.hibernate.mapping.Value;import net.sf.hibernate.mapping.PersistentClass;import net.sf.hibernate.mapping.Property;import net.sf.hibernate.mapping.Subclass;import net.sf.hibernate.metadata.ClassMetadata;import net.sf.hibernate.property.Getter;import net.sf.hibernate.property.Setter;import net.sf.hibernate.proxy.CGLIBLazyInitializer;import net.sf.hibernate.proxy.HibernateProxy;import net.sf.hibernate.sql.Alias;import net.sf.hibernate.sql.SelectFragment;import net.sf.hibernate.sql.SimpleSelect;import net.sf.hibernate.sql.Template;import net.sf.hibernate.type.AbstractComponentType;import net.sf.hibernate.type.ComponentType;import net.sf.hibernate.type.EntityType;import net.sf.hibernate.type.IdentifierType;import net.sf.hibernate.type.Type;import net.sf.hibernate.type.TypeFactory;import net.sf.hibernate.type.VersionType;import net.sf.hibernate.util.JDBCExceptionReporter;import net.sf.hibernate.util.ReflectHelper;import net.sf.hibernate.util.StringHelper;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * Superclass for built-in mapping strategies. Implements functionality * common to both mapping strategies.<br> * <br> * May be considered an immutable view of the mapping object.<br> * * @author Gavin King */public abstract class AbstractEntityPersister extends AbstractPropertyMapping implements Queryable, ClassMetadata, UniqueKeyLoadable { private static final Log log = LogFactory.getLog(AbstractEntityPersister.class); public static final String ENTITY_CLASS = "class"; protected static final Class[] NO_CLASSES = new Class[0]; private final Dialect dialect; // The class itself private final Class mappedClass; private final boolean polymorphic; private final boolean explicitPolymorphism; private final boolean inherited; private final boolean hasSubclasses; private final boolean versioned; private final boolean abstractClass; private final boolean implementsLifecycle; private final boolean implementsValidatable; private final boolean hasCollections; private final boolean hasCascades; private final boolean mutable; private final boolean selectBeforeUpdate; private final Class superclass; private final boolean dynamicUpdate; private final boolean dynamicInsert; private final int optimisticLockMode; private final String className; private final int batchSize; private final Type entityType; private final Constructor constructor; private final BulkBean optimizer; private final FastClass fastClass; // The optional SQL string defined in the where attribute private final String sqlWhereString; private final String sqlWhereStringTemplate; // proxies (if the proxies are interfaces, we use an array of // interfaces of all subclasses) private final Class[] proxyInterfaces; private final Class concreteProxyClass; private final boolean hasProxy; private final Factory proxyFactory; private final Method proxyGetIdentifierMethod; private final Method proxySetIdentifierMethod; // The SQL string used to retrieve a primary key generated // by the SQL INSERT private final boolean useIdentityColumn; private final String identitySelectString; // the identifier property private final boolean hasEmbeddedIdentifier; private final String identifierPropertyName; private final String[] identifierColumnNames; private final String[] identifierAliases; private final Cascades.IdentifierValue unsavedIdentifierValue; private final Type identifierType; private final Setter identifierSetter; private final Getter identifierGetter; private final IdentifierGenerator identifierGenerator; // version property //private final String versionPropertyName; private final String versionColumnName; private final VersionType versionType; private final Getter versionGetter; private final int versionProperty; private final Cascades.VersionValue unsavedVersionValue; // other properties (for this concrete class only, not the // subclass closure) private final int hydrateSpan; private final String[] propertyNames; private final Type[] propertyTypes; private final boolean[] propertyUpdateability; private final boolean[] propertyInsertability; private final boolean[] propertyNullability; private final Getter[] getters; private final Setter[] setters; private final Cascades.CascadeStyle[] cascadeStyles; private final HashMap gettersByPropertyName = new HashMap(); private final HashMap settersByPropertyName = new HashMap(); private final HashMap typesByPropertyName = new HashMap(); // the cache private final CacheConcurrencyStrategy cache; private final Map uniqueKeyLoaders = new HashMap(); private final Map uniqueKeyColumns = new HashMap(); private final Map subclassPropertyAliases = new HashMap(); public final Class getMappedClass() { return mappedClass; } public final String getClassName() { return className; } public String identifierSelectFragment(String name, String suffix) { return new SelectFragment() .setSuffix(suffix) .addColumns( name, getIdentifierColumnNames(), getIdentifierAliases() ) .toFragmentString() .substring(2); //strip leading ", " } public Cascades.CascadeStyle[] getPropertyCascadeStyles() { return cascadeStyles; } /** * Set the given values to the mapped properties of the given object */ public void setPropertyValues(Object object, Object[] values) throws HibernateException { try{ if (optimizer!=null) { optimizer.setPropertyValues(object, values); return; } } catch (Throwable t) { throw new PropertyAccessException( t, ReflectHelper.PROPERTY_ACCESS_EXCEPTION, true, mappedClass, ReflectHelper.getPropertyName(t, optimizer) ); } for (int j=0; j<getHydrateSpan(); j++) getSetters()[j].set(object, values[j]); } /** * Return the values of the mapped properties of the object */ public Object[] getPropertyValues(Object object) throws HibernateException { try{ if (optimizer!=null) { return optimizer.getPropertyValues(object); } } catch (Throwable t) { throw new PropertyAccessException( t, ReflectHelper.PROPERTY_ACCESS_EXCEPTION, false, mappedClass, ReflectHelper.getPropertyName(t, optimizer) ); } int span = getHydrateSpan(); Object[] result = new Object[span]; for (int j=0; j<span; j++) result[j] = getGetters()[j].get(object); return result; } /** * Get the value of the numbered property */ public Object getPropertyValue(Object object, int i) throws HibernateException { return getGetters()[i].get(object); } /** * Set the value of the numbered property */ public void setPropertyValue(Object object, int i, Object value) throws HibernateException { getSetters()[i].set(object, value); } /** * Determine if the given field values are dirty */ public int[] findDirty(Object[] x, Object[] y, Object object, SessionImplementor session) throws HibernateException { int[] props = TypeFactory.findDirty(propertyTypes, x, y, propertyUpdateability, session); if ( props==null) { return null; } else { if ( log.isTraceEnabled() ) { for ( int i=0; i<props.length; i++ ) { log.trace( StringHelper.qualify( className, propertyNames[ props[i] ] ) + " is dirty" ); } } return props; } } /** * Determine if the given field values are dirty */ public int[] findModified(Object[] old, Object[] current, Object object, SessionImplementor session) throws HibernateException { int[] props = TypeFactory.findModified(propertyTypes, old, current, propertyUpdateability, session); if ( props==null) { return null; } else { if ( log.isTraceEnabled() ) { for ( int i=0; i<props.length; i++ ) { log.trace( StringHelper.qualify( className, propertyNames[ props[i] ] ) + " is dirty" ); } } return props; } } public Serializable getIdentifier(Object object) throws HibernateException { final Object id; if (hasEmbeddedIdentifier) { id = object; } else { if (identifierGetter==null) throw new HibernateException( "The class has no identifier property: " + className ); id = identifierGetter.get(object); } try { return (Serializable) id; } catch (ClassCastException cce) { throw new ClassCastException( "Identifier classes must be serializable: " + cce.getMessage() ); } } public Object getVersion(Object object) throws HibernateException { if ( !versioned ) return null; return versionGetter.get(object); } public void setIdentifier(Object object, Serializable id) throws HibernateException { if (hasEmbeddedIdentifier) { if (object!=id) { ComponentType copier = (ComponentType) identifierType; copier.setPropertyValues( object, copier.getPropertyValues(id) ); } } else if (identifierSetter!=null) { identifierSetter.set(object, id); } } /** * Return a new instance initialized with the given identifier */ public Object instantiate(Serializable id) throws HibernateException { if ( hasEmbeddedIdentifier && id.getClass()==mappedClass ) { return id; } else { if (abstractClass) throw new HibernateException("Cannot instantiate abstract class or interface: " + className); final Object result; if (optimizer != null) { try { result = fastClass.newInstance(); } catch (Throwable t) { throw new InstantiationException("Could not instantiate entity with CGLIB: ", mappedClass, t); } } else { try { result = constructor.newInstance(null); } catch (Exception e) { throw new InstantiationException("Could not instantiate entity: ", mappedClass, e); } } setIdentifier(result, id); return result; } } // Getters and Setters protected Setter[] getSetters() { return setters; } protected Getter[] getGetters() { return getters; } public Type[] getPropertyTypes() { return propertyTypes; } public Type getIdentifierType() { return identifierType; } public String[] getIdentifierColumnNames() { return identifierColumnNames; } protected String[] getIdentifierAliases() { return identifierAliases; } public boolean isPolymorphic() { return polymorphic; } public boolean isInherited() { return inherited; } public boolean hasCascades() { return hasCascades; } public CacheConcurrencyStrategy getCache() { return cache; } public boolean hasIdentifierProperty() { return identifierGetter!=null; } public VersionType getVersionType() { return versionType; } public int getVersionProperty() { return versionProperty; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -