📄 collectiontype.java
字号:
else { id = entityEntry.getPersister().getPropertyValue( owner, foreignKeyPropertyName, session.getEntityMode() ); } // NOTE VERY HACKISH WORKAROUND!! // TODO: Fix this so it will work for non-POJO entity mode Type keyType = getPersister( session ).getKeyType(); if ( !keyType.getReturnedClass().isInstance( id ) ) { id = (Serializable) keyType.semiResolve( entityEntry.getLoadedValue( foreignKeyPropertyName ), session, owner ); } return (Serializable) id; } } /** * Get the id value from the owning entity key, usually the same as the key, but might be some * other property, in the case of property-ref * * @param key The collection owner key * @param session The session from which the request is originating. * @return The collection owner's id, if it can be obtained from the key; * otherwise, null is returned */ public Serializable getIdOfOwnerOrNull(Serializable key, SessionImplementor session) { Serializable ownerId = null; if ( foreignKeyPropertyName == null ) { ownerId = key; } else { Type keyType = getPersister( session ).getKeyType(); EntityPersister ownerPersister = getPersister( session ).getOwnerEntityPersister(); // TODO: Fix this so it will work for non-POJO entity mode Class ownerMappedClass = ownerPersister.getMappedClass( session.getEntityMode() ); if ( ownerMappedClass.isAssignableFrom( keyType.getReturnedClass() ) && keyType.getReturnedClass().isInstance( key ) ) { // the key is the owning entity itself, so get the ID from the key ownerId = ownerPersister.getIdentifier( key, session.getEntityMode() ); } else { // TODO: check if key contains the owner ID } } return ownerId; } public Object hydrate(ResultSet rs, String[] name, SessionImplementor session, Object owner) { // can't just return null here, since that would // cause an owning component to become null return NOT_NULL_COLLECTION; } public Object resolve(Object value, SessionImplementor session, Object owner) throws HibernateException { return resolveKey( getKeyOfOwner( owner, session ), session, owner ); } private Object resolveKey(Serializable key, SessionImplementor session, Object owner) { // if (key==null) throw new AssertionFailure("owner identifier unknown when re-assembling // collection reference"); return key == null ? null : // TODO: can this case really occur?? getCollection( key, session, owner ); } public Object semiResolve(Object value, SessionImplementor session, Object owner) throws HibernateException { throw new UnsupportedOperationException( "collection mappings may not form part of a property-ref" ); } public boolean isArrayType() { return false; } public boolean useLHSPrimaryKey() { return foreignKeyPropertyName == null; } public String getRHSUniqueKeyPropertyName() { return null; } public Joinable getAssociatedJoinable(SessionFactoryImplementor factory) throws MappingException { return (Joinable) factory.getCollectionPersister( role ); } public boolean isModified(Object old, Object current, boolean[] checkable, SessionImplementor session) throws HibernateException { return false; } public String getAssociatedEntityName(SessionFactoryImplementor factory) throws MappingException { try { QueryableCollection collectionPersister = (QueryableCollection) factory .getCollectionPersister( role ); if ( !collectionPersister.getElementType().isEntityType() ) { throw new MappingException( "collection was not an association: " + collectionPersister.getRole() ); } return collectionPersister.getElementPersister().getEntityName(); } catch (ClassCastException cce) { throw new MappingException( "collection role is not queryable " + role ); } } /** * Replace the elements of a collection with the elements of another collection. * * @param original The 'source' of the replacement elements (where we copy from) * @param target The target of the replacement elements (where we copy to) * @param owner The owner of the collection being merged * @param copyCache The map of elements already replaced. * @param session The session from which the merge event originated. * @return The merged collection. */ public Object replaceElements( Object original, Object target, Object owner, Map copyCache, SessionImplementor session) { // TODO: does not work for EntityMode.DOM4J yet! java.util.Collection result = ( java.util.Collection ) target; result.clear(); // copy elements into newly empty target collection Type elemType = getElementType( session.getFactory() ); Iterator iter = ( (java.util.Collection) original ).iterator(); while ( iter.hasNext() ) { result.add( elemType.replace( iter.next(), null, session, owner, copyCache ) ); } // if the original is a PersistentCollection, and that original // was not flagged as dirty, then reset the target's dirty flag // here after the copy operation. // </p> // One thing to be careful of here is a "bare" original collection // in which case we should never ever ever reset the dirty flag // on the target because we simply do not know... if ( original instanceof PersistentCollection ) { if ( result instanceof PersistentCollection ) { if ( ! ( ( PersistentCollection ) original ).isDirty() ) { ( ( PersistentCollection ) result ).clearDirty(); } } } return result; } /** * Instantiate a new "underlying" collection exhibiting the same capacity * charactersitcs and the passed "original". * * @param original The original collection. * @return The newly instantiated collection. */ protected Object instantiateResult(Object original) { // by default just use an unanticipated capacity since we don't // know how to extract the capacity to use from original here... return instantiate( -1 ); } /** * Instantiate an empty instance of the "underlying" collection (not a wrapper), * but with the given anticipated size (i.e. accounting for initial capacity * and perhaps load factor). * * @param anticipatedSize The anticipated size of the instaniated collection * after we are done populating it. * @return A newly instantiated collection to be wrapped. */ public abstract Object instantiate(int anticipatedSize); /** * {@inheritDoc} */ public Object replace( final Object original, final Object target, final SessionImplementor session, final Object owner, final Map copyCache) throws HibernateException { if ( original == null ) { return null; } if ( !Hibernate.isInitialized( original ) ) { return target; } // for a null target, or a target which is the same as the original, we // need to put the merged elements in a new collection Object result = target == null || target == original ? instantiateResult( original ) : target; //for arrays, replaceElements() may return a different reference, since //the array length might not match result = replaceElements( original, result, owner, copyCache, session ); if (original==target) { //get the elements back into the target //TODO: this is a little inefficient, don't need to do a whole // deep replaceElements() call replaceElements( result, target, owner, copyCache, session ); result = target; } return result; } /** * Get the Hibernate type of the collection elements * * @param factory The session factory. * @return The type of the collection elements * @throws MappingException Indicates the underlying persister could not be located. */ public final Type getElementType(SessionFactoryImplementor factory) throws MappingException { return factory.getCollectionPersister( getRole() ).getElementType(); } public String toString() { return getClass().getName() + '(' + getRole() + ')'; } public String getOnCondition(String alias, SessionFactoryImplementor factory, Map enabledFilters) throws MappingException { return getAssociatedJoinable( factory ).filterFragment( alias, enabledFilters ); } /** * instantiate a collection wrapper (called when loading an object) * * @param key The collection owner key * @param session The session from which the request is originating. * @param owner The collection owner * @return The collection */ public Object getCollection(Serializable key, SessionImplementor session, Object owner) { CollectionPersister persister = getPersister( session ); final PersistenceContext persistenceContext = session.getPersistenceContext(); final EntityMode entityMode = session.getEntityMode(); if (entityMode==EntityMode.DOM4J && !isEmbeddedInXML) { return UNFETCHED_COLLECTION; } // check if collection is currently being loaded PersistentCollection collection = persistenceContext.getLoadContexts().locateLoadingCollection( persister, key ); if ( collection == null ) { // check if it is already completely loaded, but unowned collection = persistenceContext.useUnownedCollection( new CollectionKey(persister, key, entityMode) ); if ( collection == null ) { // create a new collection wrapper, to be initialized later collection = instantiate( session, persister, key ); collection.setOwner(owner); persistenceContext.addUninitializedCollection( persister, collection, key ); // some collections are not lazy: if ( initializeImmediately( entityMode ) ) { session.initializeCollection( collection, false ); } else if ( !persister.isLazy() ) { persistenceContext.addNonLazyCollection( collection ); } if ( hasHolder( entityMode ) ) { session.getPersistenceContext().addCollectionHolder( collection ); } } } collection.setOwner(owner); return collection.getValue(); } public boolean hasHolder(EntityMode entityMode) { return entityMode == EntityMode.DOM4J; } protected boolean initializeImmediately(EntityMode entityMode) { return entityMode == EntityMode.DOM4J; } public String getLHSPropertyName() { return foreignKeyPropertyName; } public boolean isXMLElement() { return true; } public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException { return xml; } public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) throws HibernateException { if ( !isEmbeddedInXML ) { node.detach(); } else { replaceNode( node, (Element) value ); } } /** * We always need to dirty check the collection because we sometimes * need to incremement version number of owner and also because of * how assemble/disassemble is implemented for uks */ public boolean isAlwaysDirtyChecked() { return true; } public boolean[] toColumnNullness(Object value, Mapping mapping) { return ArrayHelper.EMPTY_BOOLEAN_ARRAY; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -