Search in sources :

Example 41 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class ModelBinder method createPluralAttribute.

private Property createPluralAttribute(MappingDocument sourceDocument, PluralAttributeSource attributeSource, PersistentClass entityDescriptor) {
    final Collection collectionBinding;
    if (attributeSource instanceof PluralAttributeSourceListImpl) {
        collectionBinding = new org.hibernate.mapping.List(sourceDocument.getMetadataCollector(), entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        registerSecondPass(new PluralAttributeListSecondPass(sourceDocument, (IndexedPluralAttributeSource) attributeSource, (org.hibernate.mapping.List) collectionBinding), sourceDocument);
    } else if (attributeSource instanceof PluralAttributeSourceSetImpl) {
        collectionBinding = new Set(sourceDocument.getMetadataCollector(), entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        registerSecondPass(new PluralAttributeSetSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
    } else if (attributeSource instanceof PluralAttributeSourceMapImpl) {
        collectionBinding = new org.hibernate.mapping.Map(sourceDocument.getMetadataCollector(), entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        registerSecondPass(new PluralAttributeMapSecondPass(sourceDocument, (IndexedPluralAttributeSource) attributeSource, (org.hibernate.mapping.Map) collectionBinding), sourceDocument);
    } else if (attributeSource instanceof PluralAttributeSourceBagImpl) {
        collectionBinding = new Bag(sourceDocument.getMetadataCollector(), entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        registerSecondPass(new PluralAttributeBagSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
    } else if (attributeSource instanceof PluralAttributeSourceIdBagImpl) {
        collectionBinding = new IdentifierBag(sourceDocument.getMetadataCollector(), entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        registerSecondPass(new PluralAttributeIdBagSecondPass(sourceDocument, attributeSource, collectionBinding), sourceDocument);
    } else if (attributeSource instanceof PluralAttributeSourceArrayImpl) {
        final PluralAttributeSourceArray arraySource = (PluralAttributeSourceArray) attributeSource;
        collectionBinding = new Array(sourceDocument.getMetadataCollector(), entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        ((Array) collectionBinding).setElementClassName(sourceDocument.qualifyClassName(arraySource.getElementClass()));
        registerSecondPass(new PluralAttributeArraySecondPass(sourceDocument, arraySource, (Array) collectionBinding), sourceDocument);
    } else if (attributeSource instanceof PluralAttributeSourcePrimitiveArrayImpl) {
        collectionBinding = new PrimitiveArray(sourceDocument.getMetadataCollector(), entityDescriptor);
        bindCollectionMetadata(sourceDocument, attributeSource, collectionBinding);
        registerSecondPass(new PluralAttributePrimitiveArraySecondPass(sourceDocument, (IndexedPluralAttributeSource) attributeSource, (PrimitiveArray) collectionBinding), sourceDocument);
    } else {
        throw new AssertionFailure("Unexpected PluralAttributeSource type : " + attributeSource.getClass().getName());
    }
    sourceDocument.getMetadataCollector().addCollectionBinding(collectionBinding);
    final Property attribute = new Property();
    attribute.setValue(collectionBinding);
    bindProperty(sourceDocument, attributeSource, attribute);
    return attribute;
}
Also used : Set(org.hibernate.mapping.Set) ArrayList(java.util.ArrayList) List(java.util.List) Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty) AssertionFailure(org.hibernate.AssertionFailure) Bag(org.hibernate.mapping.Bag) IdentifierBag(org.hibernate.mapping.IdentifierBag) IdentifierBag(org.hibernate.mapping.IdentifierBag) PrimitiveArray(org.hibernate.mapping.PrimitiveArray) PluralAttributeSourceArray(org.hibernate.boot.model.source.spi.PluralAttributeSourceArray) Array(org.hibernate.mapping.Array) PluralAttributeSourceArray(org.hibernate.boot.model.source.spi.PluralAttributeSourceArray) PrimitiveArray(org.hibernate.mapping.PrimitiveArray) Collection(org.hibernate.mapping.Collection) IndexedCollection(org.hibernate.mapping.IndexedCollection) IdentifierCollection(org.hibernate.mapping.IdentifierCollection) Map(java.util.Map) HashMap(java.util.HashMap)

Example 42 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class ModelBinder method bindCollectionMetadata.

private void bindCollectionMetadata(MappingDocument mappingDocument, PluralAttributeSource source, Collection binding) {
    binding.setRole(source.getAttributeRole().getFullPath());
    binding.setInverse(source.isInverse());
    binding.setMutable(source.isMutable());
    binding.setOptimisticLocked(source.isIncludedInOptimisticLocking());
    if (source.getCustomPersisterClassName() != null) {
        binding.setCollectionPersisterClass(mappingDocument.getClassLoaderAccess().classForName(mappingDocument.qualifyClassName(source.getCustomPersisterClassName())));
    }
    applyCaching(mappingDocument, source.getCaching(), binding);
    // bind the collection type info
    String typeName = source.getTypeInformation().getName();
    Map typeParameters = new HashMap();
    if (typeName != null) {
        // see if there is a corresponding type-def
        final TypeDefinition typeDef = mappingDocument.getMetadataCollector().getTypeDefinition(typeName);
        if (typeDef != null) {
            typeName = typeDef.getTypeImplementorClass().getName();
            if (typeDef.getParameters() != null) {
                typeParameters.putAll(typeDef.getParameters());
            }
        } else {
            // it could be a unqualified class name, in which case we should qualify
            // it with the implicit package name for this context, if one.
            typeName = mappingDocument.qualifyClassName(typeName);
        }
    }
    if (source.getTypeInformation().getParameters() != null) {
        typeParameters.putAll(source.getTypeInformation().getParameters());
    }
    binding.setTypeName(typeName);
    binding.setTypeParameters(typeParameters);
    if (source.getFetchCharacteristics().getFetchTiming() == FetchTiming.DELAYED) {
        binding.setLazy(true);
        binding.setExtraLazy(source.getFetchCharacteristics().isExtraLazy());
    } else {
        binding.setLazy(false);
    }
    switch(source.getFetchCharacteristics().getFetchStyle()) {
        case SELECT:
            {
                binding.setFetchMode(FetchMode.SELECT);
                break;
            }
        case JOIN:
            {
                binding.setFetchMode(FetchMode.JOIN);
                break;
            }
        case BATCH:
            {
                binding.setFetchMode(FetchMode.SELECT);
                binding.setBatchSize(source.getFetchCharacteristics().getBatchSize());
                break;
            }
        case SUBSELECT:
            {
                binding.setFetchMode(FetchMode.SELECT);
                binding.setSubselectLoadable(true);
                // todo : this could totally be done using a "symbol map" approach
                binding.getOwner().setSubselectLoadableCollections(true);
                break;
            }
        default:
            {
                throw new AssertionFailure("Unexpected FetchStyle : " + source.getFetchCharacteristics().getFetchStyle().name());
            }
    }
    for (String name : source.getSynchronizedTableNames()) {
        binding.getSynchronizedTables().add(name);
    }
    binding.setWhere(source.getWhere());
    binding.setLoaderName(source.getCustomLoaderName());
    if (source.getCustomSqlInsert() != null) {
        binding.setCustomSQLInsert(source.getCustomSqlInsert().getSql(), source.getCustomSqlInsert().isCallable(), source.getCustomSqlInsert().getCheckStyle());
    }
    if (source.getCustomSqlUpdate() != null) {
        binding.setCustomSQLUpdate(source.getCustomSqlUpdate().getSql(), source.getCustomSqlUpdate().isCallable(), source.getCustomSqlUpdate().getCheckStyle());
    }
    if (source.getCustomSqlDelete() != null) {
        binding.setCustomSQLDelete(source.getCustomSqlDelete().getSql(), source.getCustomSqlDelete().isCallable(), source.getCustomSqlDelete().getCheckStyle());
    }
    if (source.getCustomSqlDeleteAll() != null) {
        binding.setCustomSQLDeleteAll(source.getCustomSqlDeleteAll().getSql(), source.getCustomSqlDeleteAll().isCallable(), source.getCustomSqlDeleteAll().getCheckStyle());
    }
    if (source instanceof Sortable) {
        final Sortable sortable = (Sortable) source;
        if (sortable.isSorted()) {
            binding.setSorted(true);
            if (!sortable.getComparatorName().equals("natural")) {
                binding.setComparatorClassName(sortable.getComparatorName());
            }
        } else {
            binding.setSorted(false);
        }
    }
    if (source instanceof Orderable) {
        if (((Orderable) source).isOrdered()) {
            binding.setOrderBy(((Orderable) source).getOrder());
        }
    }
    final String cascadeStyle = source.getCascadeStyleName();
    if (cascadeStyle != null && cascadeStyle.contains("delete-orphan")) {
        binding.setOrphanDelete(true);
    }
    for (FilterSource filterSource : source.getFilterSources()) {
        String condition = filterSource.getCondition();
        if (condition == null) {
            final FilterDefinition filterDefinition = mappingDocument.getMetadataCollector().getFilterDefinition(filterSource.getName());
            if (filterDefinition != null) {
                condition = filterDefinition.getDefaultFilterCondition();
            }
        }
        binding.addFilter(filterSource.getName(), condition, filterSource.shouldAutoInjectAliases(), filterSource.getAliasToTableMap(), filterSource.getAliasToEntityMap());
    }
}
Also used : FilterDefinition(org.hibernate.engine.spi.FilterDefinition) AssertionFailure(org.hibernate.AssertionFailure) FilterSource(org.hibernate.boot.model.source.spi.FilterSource) HashMap(java.util.HashMap) Sortable(org.hibernate.boot.model.source.spi.Sortable) Orderable(org.hibernate.boot.model.source.spi.Orderable) Map(java.util.Map) HashMap(java.util.HashMap) TypeDefinition(org.hibernate.boot.model.TypeDefinition)

Example 43 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class StatementPreparerImpl method prepareQueryStatement.

@Override
public PreparedStatement prepareQueryStatement(String sql, final boolean isCallable, final ScrollMode scrollMode) {
    if (scrollMode != null && !scrollMode.equals(ScrollMode.FORWARD_ONLY)) {
        if (!settings().isScrollableResultSetsEnabled()) {
            throw new AssertionFailure("scrollable result sets are not enabled");
        }
        final PreparedStatement ps = new QueryStatementPreparationTemplate(sql) {

            public PreparedStatement doPrepare() throws SQLException {
                return isCallable ? connection().prepareCall(sql, scrollMode.toResultSetType(), ResultSet.CONCUR_READ_ONLY) : connection().prepareStatement(sql, scrollMode.toResultSetType(), ResultSet.CONCUR_READ_ONLY);
            }
        }.prepareStatement();
        jdbcCoordinator.registerLastQuery(ps);
        return ps;
    } else {
        final PreparedStatement ps = new QueryStatementPreparationTemplate(sql) {

            public PreparedStatement doPrepare() throws SQLException {
                return isCallable ? connection().prepareCall(sql) : connection().prepareStatement(sql);
            }
        }.prepareStatement();
        jdbcCoordinator.registerLastQuery(ps);
        return ps;
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement)

Example 44 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class DefaultSaveOrUpdateEventListener method entityIsDetached.

/**
	 * The given save-update event named a detached entity.
	 * <p/>
	 * Here, we will perform the update processing.
	 *
	 * @param event The update event to be handled.
	 */
protected void entityIsDetached(SaveOrUpdateEvent event) {
    LOG.trace("Updating detached instance");
    if (event.getSession().getPersistenceContext().isEntryFor(event.getEntity())) {
        //TODO: assertion only, could be optimized away
        throw new AssertionFailure("entity was persistent");
    }
    Object entity = event.getEntity();
    EntityPersister persister = event.getSession().getEntityPersister(event.getEntityName(), entity);
    event.setRequestedId(getUpdateId(entity, persister, event.getRequestedId(), event.getSession()));
    performUpdate(event, entity, persister);
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) AssertionFailure(org.hibernate.AssertionFailure)

Example 45 with AssertionFailure

use of org.hibernate.AssertionFailure in project hibernate-orm by hibernate.

the class DefaultSaveOrUpdateEventListener method entityIsPersistent.

protected Serializable entityIsPersistent(SaveOrUpdateEvent event) throws HibernateException {
    final boolean traceEnabled = LOG.isTraceEnabled();
    if (traceEnabled) {
        LOG.trace("Ignoring persistent instance");
    }
    EntityEntry entityEntry = event.getEntry();
    if (entityEntry == null) {
        throw new AssertionFailure("entity was transient or detached");
    } else {
        if (entityEntry.getStatus() == Status.DELETED) {
            throw new AssertionFailure("entity was deleted");
        }
        final SessionFactoryImplementor factory = event.getSession().getFactory();
        Serializable requestedId = event.getRequestedId();
        Serializable savedId;
        if (requestedId == null) {
            savedId = entityEntry.getId();
        } else {
            final boolean isEqual = !entityEntry.getPersister().getIdentifierType().isEqual(requestedId, entityEntry.getId(), factory);
            if (isEqual) {
                throw new PersistentObjectException("object passed to save() was already persistent: " + MessageHelper.infoString(entityEntry.getPersister(), requestedId, factory));
            }
            savedId = requestedId;
        }
        if (traceEnabled) {
            LOG.tracev("Object already associated with session: {0}", MessageHelper.infoString(entityEntry.getPersister(), savedId, factory));
        }
        return savedId;
    }
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) Serializable(java.io.Serializable) AssertionFailure(org.hibernate.AssertionFailure) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PersistentObjectException(org.hibernate.PersistentObjectException)

Aggregations

AssertionFailure (org.hibernate.AssertionFailure)54 EntityEntry (org.hibernate.engine.spi.EntityEntry)11 Serializable (java.io.Serializable)10 Property (org.hibernate.mapping.Property)10 AnnotationException (org.hibernate.AnnotationException)8 PersistentClass (org.hibernate.mapping.PersistentClass)7 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 SQLException (java.sql.SQLException)6 HibernateException (org.hibernate.HibernateException)6 SimpleValue (org.hibernate.mapping.SimpleValue)6 PreparedStatement (java.sql.PreparedStatement)5 HashMap (java.util.HashMap)5 Component (org.hibernate.mapping.Component)5 Join (org.hibernate.mapping.Join)5 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)5 Iterator (java.util.Iterator)4 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)4 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)4 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)4 ManyToOne (org.hibernate.mapping.ManyToOne)4