📄 entitytype.java
字号:
return persister.getIdentifierType() .isEqual(xid, yid, entityMode, factory); } /** * {@inheritDoc} */ public boolean isEmbeddedInXML() { return isEmbeddedInXML; } /** * {@inheritDoc} */ public boolean isXMLElement() { return isEmbeddedInXML; } /** * {@inheritDoc} */ public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException { if ( !isEmbeddedInXML ) { return getIdentifierType(factory).fromXMLNode(xml, factory); } else { return xml; } } /** * {@inheritDoc} */ public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) throws HibernateException { if ( !isEmbeddedInXML ) { getIdentifierType(factory).setToXMLNode(node, value, factory); } else { Element elt = (Element) value; replaceNode( node, new ElementWrapper(elt) ); } } public String getOnCondition(String alias, SessionFactoryImplementor factory, Map enabledFilters) throws MappingException { if ( isReferenceToPrimaryKey() ) { //TODO: this is a bit arbitrary, expose a switch to the user? return ""; } else { return getAssociatedJoinable( factory ).filterFragment( alias, enabledFilters ); } } /** * Resolve an identifier or unique key value */ public Object resolve(Object value, SessionImplementor session, Object owner) throws HibernateException { if ( isNotEmbedded( session ) ) { return value; } if ( value == null ) { return null; } else { if ( isNull( owner, session ) ) { return null; //EARLY EXIT! } if ( isReferenceToPrimaryKey() ) { return resolveIdentifier( (Serializable) value, session ); } else { return loadByUniqueKey( getAssociatedEntityName(), uniqueKeyPropertyName, value, session ); } } } public Type getSemiResolvedType(SessionFactoryImplementor factory) { return factory.getEntityPersister( associatedEntityName ).getIdentifierType(); } protected final Object getIdentifier(Object value, SessionImplementor session) throws HibernateException { if ( isNotEmbedded(session) ) { return value; } if ( isReferenceToPrimaryKey() ) { return ForeignKeys.getEntityIdentifierIfNotUnsaved( getAssociatedEntityName(), value, session ); //tolerates nulls } else if ( value == null ) { return null; } else { EntityPersister entityPersister = session.getFactory().getEntityPersister( getAssociatedEntityName() ); Object propertyValue = entityPersister.getPropertyValue( value, uniqueKeyPropertyName, session.getEntityMode() ); // We now have the value of the property-ref we reference. However, // we need to dig a little deeper, as that property might also be // an entity type, in which case we need to resolve its identitifier Type type = entityPersister.getPropertyType( uniqueKeyPropertyName ); if ( type.isEntityType() ) { propertyValue = ( ( EntityType ) type ).getIdentifier( propertyValue, session ); } return propertyValue; } } protected boolean isNotEmbedded(SessionImplementor session) { return !isEmbeddedInXML && session.getEntityMode()==EntityMode.DOM4J; } /** * Get the identifier value of an instance or proxy. * <p/> * Intended only for loggin purposes!!! * * @param object The object from which to extract the identifier. * @param persister The entity persister * @param entityMode The entity mode * @return The extracted identifier. */ private static Serializable getIdentifier(Object object, EntityPersister persister, EntityMode entityMode) { if (object instanceof HibernateProxy) { HibernateProxy proxy = (HibernateProxy) object; LazyInitializer li = proxy.getHibernateLazyInitializer(); return li.getIdentifier(); } else { return persister.getIdentifier( object, entityMode ); } } /** * Generate a loggable representation of an instance of the value mapped by this type. * * @param value The instance to be logged. * @param factory The session factory. * @return The loggable string. * @throws HibernateException Generally some form of resolution problem. */ public String toLoggableString(Object value, SessionFactoryImplementor factory) { if ( value == null ) { return "null"; } EntityPersister persister = factory.getEntityPersister( associatedEntityName ); StringBuffer result = new StringBuffer().append( associatedEntityName ); if ( persister.hasIdentifierProperty() ) { final EntityMode entityMode = persister.guessEntityMode( value ); final Serializable id; if ( entityMode == null ) { if ( isEmbeddedInXML ) { throw new ClassCastException( value.getClass().getName() ); } id = ( Serializable ) value; } else { id = getIdentifier( value, persister, entityMode ); } result.append( '#' ) .append( persister.getIdentifierType().toLoggableString( id, factory ) ); } return result.toString(); } public abstract boolean isOneToOne(); /** * Convenience method to locate the identifier type of the associated entity. * * @param factory The mappings... * @return The identifier type */ Type getIdentifierType(Mapping factory) { return factory.getIdentifierType( getAssociatedEntityName() ); } /** * Convenience method to locate the identifier type of the associated entity. * * @param session The originating session * @return The identifier type */ Type getIdentifierType(SessionImplementor session) { return getIdentifierType( session.getFactory() ); } /** * Determine the type of either (1) the identifier if we reference the * associated entity's PK or (2) the unique key to which we refer (i.e. * the property-ref). * * @param factory The mappings... * @return The appropriate type. * @throws MappingException Generally, if unable to resolve the associated entity name * or unique key property name. */ public final Type getIdentifierOrUniqueKeyType(Mapping factory) throws MappingException { if ( isReferenceToPrimaryKey() ) { return getIdentifierType(factory); } else { Type type = factory.getReferencedPropertyType( getAssociatedEntityName(), uniqueKeyPropertyName ); if ( type.isEntityType() ) { type = ( ( EntityType ) type).getIdentifierOrUniqueKeyType( factory ); } return type; } } /** * The name of the property on the associated entity to which our FK * refers * * @param factory The mappings... * @return The appropriate property name. * @throws MappingException Generally, if unable to resolve the associated entity name */ public final String getIdentifierOrUniqueKeyPropertyName(Mapping factory) throws MappingException { if ( isReferenceToPrimaryKey() ) { return factory.getIdentifierPropertyName( getAssociatedEntityName() ); } else { return uniqueKeyPropertyName; } } protected abstract boolean isNullable(); /** * Resolve an identifier via a load. * * @param id The entity id to resolve * @param session The orginating session. * @return The resolved identifier (i.e., loaded entity). * @throws org.hibernate.HibernateException Indicates problems performing the load. */ protected final Object resolveIdentifier(Serializable id, SessionImplementor session) throws HibernateException { boolean isProxyUnwrapEnabled = unwrapProxy && session.getFactory() .getEntityPersister( getAssociatedEntityName() ) .isInstrumented( session.getEntityMode() ); Object proxyOrEntity = session.internalLoad( getAssociatedEntityName(), id, eager, isNullable() && !isProxyUnwrapEnabled ); if ( proxyOrEntity instanceof HibernateProxy ) { ( ( HibernateProxy ) proxyOrEntity ).getHibernateLazyInitializer() .setUnwrap( isProxyUnwrapEnabled ); } return proxyOrEntity; } protected boolean isNull(Object owner, SessionImplementor session) { return false; } /** * Load an instance by a unique key that is not the primary key. * * @param entityName The name of the entity to load * @param uniqueKeyPropertyName The name of the property defining the uniqie key. * @param key The unique key property value. * @param session The originating session. * @return The loaded entity * @throws HibernateException generally indicates problems performing the load. */ public Object loadByUniqueKey( String entityName, String uniqueKeyPropertyName, Object key, SessionImplementor session) throws HibernateException { final SessionFactoryImplementor factory = session.getFactory(); UniqueKeyLoadable persister = ( UniqueKeyLoadable ) factory.getEntityPersister( entityName ); //TODO: implement caching?! proxies?! EntityUniqueKey euk = new EntityUniqueKey( entityName, uniqueKeyPropertyName, key, getIdentifierOrUniqueKeyType( factory ), session.getEntityMode(), session.getFactory() ); final PersistenceContext persistenceContext = session.getPersistenceContext(); Object result = persistenceContext.getEntity( euk ); if ( result == null ) { result = persister.loadByUniqueKey( uniqueKeyPropertyName, key, session ); } return result == null ? null : persistenceContext.proxyFor( result ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -