Search in sources :

Example 1 with LazyAttributeDescriptor

use of org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeDescriptor in project hibernate-orm by hibernate.

the class AbstractEntityPersister method generateLazySelectStringsByFetchGroup.

protected Map<String, String> generateLazySelectStringsByFetchGroup() {
    final BytecodeEnhancementMetadata enhancementMetadata = entityMetamodel.getBytecodeEnhancementMetadata();
    if (!enhancementMetadata.isEnhancedForLazyLoading() || !enhancementMetadata.getLazyAttributesMetadata().hasLazyAttributes()) {
        return Collections.emptyMap();
    }
    Map<String, String> result = new HashMap<>();
    final LazyAttributesMetadata lazyAttributesMetadata = enhancementMetadata.getLazyAttributesMetadata();
    for (String groupName : lazyAttributesMetadata.getFetchGroupNames()) {
        HashSet tableNumbers = new HashSet();
        ArrayList columnNumbers = new ArrayList();
        ArrayList formulaNumbers = new ArrayList();
        for (LazyAttributeDescriptor lazyAttributeDescriptor : lazyAttributesMetadata.getFetchGroupAttributeDescriptors(groupName)) {
            // all this only really needs to consider properties
            // of this class, not its subclasses, but since we
            // are reusing code used for sequential selects, we
            // use the subclass closure
            int propertyNumber = getSubclassPropertyIndex(lazyAttributeDescriptor.getName());
            int tableNumber = getSubclassPropertyTableNumber(propertyNumber);
            tableNumbers.add(tableNumber);
            int[] colNumbers = subclassPropertyColumnNumberClosure[propertyNumber];
            for (int colNumber : colNumbers) {
                if (colNumber != -1) {
                    columnNumbers.add(colNumber);
                }
            }
            int[] formNumbers = subclassPropertyFormulaNumberClosure[propertyNumber];
            for (int formNumber : formNumbers) {
                if (formNumber != -1) {
                    formulaNumbers.add(formNumber);
                }
            }
        }
        if (columnNumbers.size() == 0 && formulaNumbers.size() == 0) {
            // only one-to-one is lazy fetched
            continue;
        }
        result.put(groupName, renderSelect(ArrayHelper.toIntArray(tableNumbers), ArrayHelper.toIntArray(columnNumbers), ArrayHelper.toIntArray(formulaNumbers)));
    }
    return result;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LazyAttributesMetadata(org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributesMetadata) ArrayList(java.util.ArrayList) BytecodeEnhancementMetadata(org.hibernate.bytecode.spi.BytecodeEnhancementMetadata) LazyAttributeDescriptor(org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeDescriptor) HashSet(java.util.HashSet)

Example 2 with LazyAttributeDescriptor

use of org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeDescriptor in project hibernate-orm by hibernate.

the class AbstractEntityPersister method initializeLazyPropertiesFromDatastore.

private Object initializeLazyPropertiesFromDatastore(final String fieldName, final Object entity, final SharedSessionContractImplementor session, final Serializable id, final EntityEntry entry) {
    if (!hasLazyProperties()) {
        throw new AssertionFailure("no lazy properties");
    }
    final InterceptorImplementor interceptor = ((PersistentAttributeInterceptable) entity).$$_hibernate_getInterceptor();
    assert interceptor != null : "Expecting bytecode interceptor to be non-null";
    LOG.trace("Initializing lazy properties from datastore");
    final String fetchGroup = getEntityMetamodel().getBytecodeEnhancementMetadata().getLazyAttributesMetadata().getFetchGroupName(fieldName);
    final List<LazyAttributeDescriptor> fetchGroupAttributeDescriptors = getEntityMetamodel().getBytecodeEnhancementMetadata().getLazyAttributesMetadata().getFetchGroupAttributeDescriptors(fetchGroup);
    final Set<String> initializedLazyAttributeNames = interceptor.getInitializedLazyAttributeNames();
    final String lazySelect = getSQLLazySelectString(fetchGroup);
    try {
        Object result = null;
        PreparedStatement ps = null;
        try {
            ResultSet rs = null;
            try {
                if (lazySelect != null) {
                    // null sql means that the only lazy properties
                    // are shared PK one-to-one associations which are
                    // handled differently in the Type#nullSafeGet code...
                    ps = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(lazySelect);
                    getIdentifierType().nullSafeSet(ps, id, 1, session);
                    rs = session.getJdbcCoordinator().getResultSetReturn().extract(ps);
                    rs.next();
                }
                final Object[] snapshot = entry.getLoadedState();
                for (LazyAttributeDescriptor fetchGroupAttributeDescriptor : fetchGroupAttributeDescriptors) {
                    final boolean previousInitialized = initializedLazyAttributeNames.contains(fetchGroupAttributeDescriptor.getName());
                    if (previousInitialized) {
                        // its already been initialized (e.g. by a write) so we don't want to overwrite
                        continue;
                    }
                    final Object selectedValue = fetchGroupAttributeDescriptor.getType().nullSafeGet(rs, lazyPropertyColumnAliases[fetchGroupAttributeDescriptor.getLazyIndex()], session, entity);
                    final boolean set = initializeLazyProperty(fieldName, entity, session, snapshot, fetchGroupAttributeDescriptor.getLazyIndex(), selectedValue);
                    if (set) {
                        result = selectedValue;
                        interceptor.attributeInitialized(fetchGroupAttributeDescriptor.getName());
                    }
                }
            } finally {
                if (rs != null) {
                    session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(rs, ps);
                }
            }
        } finally {
            if (ps != null) {
                session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(ps);
                session.getJdbcCoordinator().afterStatementExecution();
            }
        }
        LOG.trace("Done initializing lazy properties");
        return result;
    } catch (SQLException sqle) {
        throw session.getJdbcServices().getSqlExceptionHelper().convert(sqle, "could not initialize lazy properties: " + MessageHelper.infoString(this, id, getFactory()), lazySelect);
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) SQLException(java.sql.SQLException) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) LazyAttributeDescriptor(org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeDescriptor)

Aggregations

LazyAttributeDescriptor (org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeDescriptor)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 AssertionFailure (org.hibernate.AssertionFailure)1 LazyAttributesMetadata (org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributesMetadata)1 BytecodeEnhancementMetadata (org.hibernate.bytecode.spi.BytecodeEnhancementMetadata)1 PersistentAttributeInterceptable (org.hibernate.engine.spi.PersistentAttributeInterceptable)1