Search in sources :

Example 21 with AssertionFailure

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

the class StandardForeignKeyExporter method getSqlCreateStrings.

@Override
public String[] getSqlCreateStrings(ForeignKey foreignKey, Metadata metadata) {
    if (!dialect.hasAlterTable()) {
        return NO_COMMANDS;
    }
    if (!foreignKey.isCreationEnabled()) {
        return NO_COMMANDS;
    }
    if (!foreignKey.isPhysicalConstraint()) {
        return NO_COMMANDS;
    }
    final int numberOfColumns = foreignKey.getColumnSpan();
    final String[] columnNames = new String[numberOfColumns];
    final String[] targetColumnNames = new String[numberOfColumns];
    final Iterator targetItr;
    if (foreignKey.isReferenceToPrimaryKey()) {
        if (numberOfColumns != foreignKey.getReferencedTable().getPrimaryKey().getColumnSpan()) {
            throw new AssertionFailure(String.format(Locale.ENGLISH, COLUMN_MISMATCH_MSG, numberOfColumns, foreignKey.getReferencedTable().getPrimaryKey().getColumnSpan(), foreignKey.getName(), foreignKey.getTable().getName(), foreignKey.getReferencedTable().getName()));
        }
        targetItr = foreignKey.getReferencedTable().getPrimaryKey().getColumnIterator();
    } else {
        if (numberOfColumns != foreignKey.getReferencedColumns().size()) {
            throw new AssertionFailure(String.format(Locale.ENGLISH, COLUMN_MISMATCH_MSG, numberOfColumns, foreignKey.getReferencedColumns().size(), foreignKey.getName(), foreignKey.getTable().getName(), foreignKey.getReferencedTable().getName()));
        }
        targetItr = foreignKey.getReferencedColumns().iterator();
    }
    int i = 0;
    final Iterator itr = foreignKey.getColumnIterator();
    while (itr.hasNext()) {
        columnNames[i] = ((Column) itr.next()).getQuotedName(dialect);
        targetColumnNames[i] = ((Column) targetItr.next()).getQuotedName(dialect);
        i++;
    }
    final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
    final String sourceTableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(foreignKey.getTable().getQualifiedTableName(), dialect);
    final String targetTableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(foreignKey.getReferencedTable().getQualifiedTableName(), dialect);
    final StringBuilder buffer = new StringBuilder("alter table ").append(sourceTableName).append(foreignKey.getKeyDefinition() != null ? dialect.getAddForeignKeyConstraintString(foreignKey.getName(), foreignKey.getKeyDefinition()) : dialect.getAddForeignKeyConstraintString(foreignKey.getName(), columnNames, targetTableName, targetColumnNames, foreignKey.isReferenceToPrimaryKey()));
    if (dialect.supportsCascadeDelete()) {
        if (foreignKey.isCascadeDeleteEnabled()) {
            buffer.append(" on delete cascade");
        }
    }
    return new String[] { buffer.toString() };
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) Iterator(java.util.Iterator) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)

Example 22 with AssertionFailure

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

the class InterceptorTest method testPrepareStatementFaultIntercept.

public void testPrepareStatementFaultIntercept() {
    final Interceptor interceptor = new EmptyInterceptor() {

        @Override
        public String onPrepareStatement(String sql) {
            return null;
        }
    };
    Session s = openSession(interceptor);
    try {
        Transaction t = s.beginTransaction();
        User u = new User("Kinga", "Mroz");
        s.persist(u);
        t.commit();
    } catch (TransactionException e) {
        assertTrue(e.getCause() instanceof AssertionFailure);
    } finally {
        s.close();
    }
}
Also used : TransactionException(org.hibernate.TransactionException) AssertionFailure(org.hibernate.AssertionFailure) Transaction(org.hibernate.Transaction) EmptyInterceptor(org.hibernate.EmptyInterceptor) Interceptor(org.hibernate.Interceptor) EmptyInterceptor(org.hibernate.EmptyInterceptor) Session(org.hibernate.Session)

Example 23 with AssertionFailure

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

the class ModelBinder method bindAllCompositeAttributes.

private void bindAllCompositeAttributes(MappingDocument sourceDocument, EmbeddableSource embeddableSource, Component component) {
    for (AttributeSource attributeSource : embeddableSource.attributeSources()) {
        Property attribute = null;
        if (SingularAttributeSourceBasic.class.isInstance(attributeSource)) {
            attribute = createBasicAttribute(sourceDocument, (SingularAttributeSourceBasic) attributeSource, new SimpleValue(sourceDocument.getMetadataCollector(), component.getTable()), component.getComponentClassName());
        } else if (SingularAttributeSourceEmbedded.class.isInstance(attributeSource)) {
            attribute = createEmbeddedAttribute(sourceDocument, (SingularAttributeSourceEmbedded) attributeSource, new Component(sourceDocument.getMetadataCollector(), component), component.getComponentClassName());
        } else if (SingularAttributeSourceManyToOne.class.isInstance(attributeSource)) {
            attribute = createManyToOneAttribute(sourceDocument, (SingularAttributeSourceManyToOne) attributeSource, new ManyToOne(sourceDocument.getMetadataCollector(), component.getTable()), component.getComponentClassName());
        } else if (SingularAttributeSourceOneToOne.class.isInstance(attributeSource)) {
            attribute = createOneToOneAttribute(sourceDocument, (SingularAttributeSourceOneToOne) attributeSource, new OneToOne(sourceDocument.getMetadataCollector(), component.getTable(), component.getOwner()), component.getComponentClassName());
        } else if (SingularAttributeSourceAny.class.isInstance(attributeSource)) {
            attribute = createAnyAssociationAttribute(sourceDocument, (SingularAttributeSourceAny) attributeSource, new Any(sourceDocument.getMetadataCollector(), component.getTable()), component.getComponentClassName());
        } else if (PluralAttributeSource.class.isInstance(attributeSource)) {
            attribute = createPluralAttribute(sourceDocument, (PluralAttributeSource) attributeSource, component.getOwner());
        } else {
            throw new AssertionFailure(String.format(Locale.ENGLISH, "Unexpected AttributeSource sub-type [%s] as part of composite [%s]", attributeSource.getClass().getName(), attributeSource.getAttributeRole().getFullPath()));
        }
        component.addProperty(attribute);
    }
}
Also used : SingularAttributeSourceBasic(org.hibernate.boot.model.source.spi.SingularAttributeSourceBasic) VersionAttributeSource(org.hibernate.boot.model.source.spi.VersionAttributeSource) AttributeSource(org.hibernate.boot.model.source.spi.AttributeSource) PluralAttributeSource(org.hibernate.boot.model.source.spi.PluralAttributeSource) SingularAttributeSource(org.hibernate.boot.model.source.spi.SingularAttributeSource) SingularAttributeSourceAny(org.hibernate.boot.model.source.spi.SingularAttributeSourceAny) AssertionFailure(org.hibernate.AssertionFailure) SingularAttributeSourceManyToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceManyToOne) SingularAttributeSourceOneToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceOneToOne) PluralAttributeElementSourceManyToAny(org.hibernate.boot.model.source.spi.PluralAttributeElementSourceManyToAny) Any(org.hibernate.mapping.Any) SingularAttributeSourceAny(org.hibernate.boot.model.source.spi.SingularAttributeSourceAny) SingularAttributeSourceEmbedded(org.hibernate.boot.model.source.spi.SingularAttributeSourceEmbedded) SingularAttributeSourceManyToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceManyToOne) ManyToOne(org.hibernate.mapping.ManyToOne) SimpleValue(org.hibernate.mapping.SimpleValue) PluralAttributeSource(org.hibernate.boot.model.source.spi.PluralAttributeSource) OneToOne(org.hibernate.mapping.OneToOne) SingularAttributeSourceOneToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceOneToOne) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty)

Example 24 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 25 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)

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