entitytype.java
来自「hibernate-distribution-3.3.1.GA-dist.zip」· Java 代码 · 共 647 行 · 第 1/2 页
JAVA
647 行
/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Middleware LLC. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA * */package org.hibernate.type;import java.io.Serializable;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Map;import org.dom4j.Element;import org.dom4j.Node;import org.hibernate.AssertionFailure;import org.hibernate.EntityMode;import org.hibernate.HibernateException;import org.hibernate.MappingException;import org.hibernate.engine.EntityUniqueKey;import org.hibernate.engine.ForeignKeys;import org.hibernate.engine.Mapping;import org.hibernate.engine.PersistenceContext;import org.hibernate.engine.SessionFactoryImplementor;import org.hibernate.engine.SessionImplementor;import org.hibernate.persister.entity.EntityPersister;import org.hibernate.persister.entity.Joinable;import org.hibernate.persister.entity.UniqueKeyLoadable;import org.hibernate.proxy.HibernateProxy;import org.hibernate.proxy.LazyInitializer;import org.hibernate.tuple.ElementWrapper;import org.hibernate.util.ReflectHelper;/** * Base for types which map associations to persistent entities. * * @author Gavin King */public abstract class EntityType extends AbstractType implements AssociationType { private final String associatedEntityName; protected final String uniqueKeyPropertyName; protected final boolean isEmbeddedInXML; private final boolean eager; private final boolean unwrapProxy; private transient Class returnedClass; /** * Constructs the requested entity type mapping. * * @param entityName The name of the associated entity. * @param uniqueKeyPropertyName The property-ref name, or null if we * reference the PK of the associated entity. * @param eager Is eager fetching enabled. * @param isEmbeddedInXML Should values of this mapping be embedded in XML modes? * @param unwrapProxy Is unwrapping of proxies allowed for this association; unwrapping * says to return the "implementation target" of lazy prooxies; typically only possible * with lazy="no-proxy". */ protected EntityType( String entityName, String uniqueKeyPropertyName, boolean eager, boolean isEmbeddedInXML, boolean unwrapProxy) { this.associatedEntityName = entityName; this.uniqueKeyPropertyName = uniqueKeyPropertyName; this.isEmbeddedInXML = isEmbeddedInXML; this.eager = eager; this.unwrapProxy = unwrapProxy; } /** * An entity type is a type of association type * * @return True. */ public boolean isAssociationType() { return true; } /** * Explicitly, an entity type is an entity type ;) * * @return True. */ public final boolean isEntityType() { return true; } /** * {@inheritDoc} */ public boolean isMutable() { return false; } /** * Generates a string representation of this type. * * @return string rep */ public String toString() { return getClass().getName() + '(' + getAssociatedEntityName() + ')'; } /** * For entity types, the name correlates to the associated entity name. */ public String getName() { return associatedEntityName; } /** * Does this association foreign key reference the primary key of the other table? * Otherwise, it references a property-ref. * * @return True if this association reference the PK of the associated entity. */ public boolean isReferenceToPrimaryKey() { return uniqueKeyPropertyName==null; } public String getRHSUniqueKeyPropertyName() { return uniqueKeyPropertyName; } public String getLHSPropertyName() { return null; } public String getPropertyName() { return null; } /** * The name of the associated entity. * * @return The associated entity name. */ public final String getAssociatedEntityName() { return associatedEntityName; } /** * The name of the associated entity. * * @param factory The session factory, for resolution. * @return The associated entity name. */ public String getAssociatedEntityName(SessionFactoryImplementor factory) { return getAssociatedEntityName(); } /** * Retrieves the {@link Joinable} defining the associated entity. * * @param factory The session factory. * @return The associated joinable * @throws MappingException Generally indicates an invalid entity name. */ public Joinable getAssociatedJoinable(SessionFactoryImplementor factory) throws MappingException { return ( Joinable ) factory.getEntityPersister( associatedEntityName ); } /** * This returns the wrong class for an entity with a proxy, or for a named * entity. Theoretically it should return the proxy class, but it doesn't. * <p/> * The problem here is that we do not necessarily have a ref to the associated * entity persister (nor to the session factory, to look it up) which is really * needed to "do the right thing" here... * * @return The entiyt class. */ public final Class getReturnedClass() { if ( returnedClass == null ) { returnedClass = determineAssociatedEntityClass(); } return returnedClass; } private Class determineAssociatedEntityClass() { try { return ReflectHelper.classForName( getAssociatedEntityName() ); } catch ( ClassNotFoundException cnfe ) { return java.util.Map.class; } } /** * {@inheritDoc} */ public Object nullSafeGet(ResultSet rs, String name, SessionImplementor session, Object owner) throws HibernateException, SQLException { return nullSafeGet( rs, new String[] {name}, session, owner ); } /** * {@inheritDoc} */ public final Object nullSafeGet( ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { return resolve( hydrate(rs, names, session, owner), session, owner ); } /** * Two entities are considered the same when their instances are the same. * * @param x One entity instance * @param y Another entity instance * @param entityMode The entity mode. * @return True if x == y; false otherwise. */ public final boolean isSame(Object x, Object y, EntityMode entityMode) { return x == y; } /** * {@inheritDoc} */ public int compare(Object x, Object y, EntityMode entityMode) { return 0; //TODO: entities CAN be compared, by PK, fix this! -> only if/when we can extract the id values.... } /** * {@inheritDoc} */ public Object deepCopy(Object value, EntityMode entityMode, SessionFactoryImplementor factory) { return value; //special case ... this is the leaf of the containment graph, even though not immutable } /** * {@inheritDoc} */ public Object replace( Object original, Object target, SessionImplementor session, Object owner, Map copyCache) throws HibernateException { if ( original == null ) { return null; } Object cached = copyCache.get(original); if ( cached != null ) { return cached; } else { if ( original == target ) { return target; } if ( session.getContextEntityIdentifier( original ) == null && ForeignKeys.isTransient( associatedEntityName, original, Boolean.FALSE, session ) ) { final Object copy = session.getFactory().getEntityPersister( associatedEntityName ) .instantiate( null, session.getEntityMode() ); //TODO: should this be Session.instantiate(Persister, ...)? copyCache.put( original, copy ); return copy; } else { Object id = getIdentifier( original, session ); if ( id == null ) { throw new AssertionFailure("non-transient entity has a null id"); } id = getIdentifierOrUniqueKeyType( session.getFactory() ) .replace(id, null, session, owner, copyCache); return resolve( id, session, owner ); } } } /** * {@inheritDoc} */ public int getHashCode(Object x, EntityMode entityMode, SessionFactoryImplementor factory) { EntityPersister persister = factory.getEntityPersister(associatedEntityName); if ( !persister.canExtractIdOutOfEntity() ) { return super.getHashCode(x, entityMode); } final Serializable id; if (x instanceof HibernateProxy) { id = ( (HibernateProxy) x ).getHibernateLazyInitializer().getIdentifier(); } else { id = persister.getIdentifier(x, entityMode); } return persister.getIdentifierType().getHashCode(id, entityMode, factory); } /** * {@inheritDoc} */ public boolean isEqual(Object x, Object y, EntityMode entityMode, SessionFactoryImplementor factory) { EntityPersister persister = factory.getEntityPersister(associatedEntityName); if ( !persister.canExtractIdOutOfEntity() ) { return super.isEqual(x, y, entityMode); } Serializable xid;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?