Search in sources :

Example 36 with AssertionFailure

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

the class Collections method prepareCollectionForUpdate.

/**
	 * 1. record the collection role that this collection is referenced by
	 * 2. decide if the collection needs deleting/creating/updating (but
	 *	don't actually schedule the action yet)
	 */
@SuppressWarnings({ "JavaDoc" })
private static void prepareCollectionForUpdate(PersistentCollection collection, CollectionEntry entry, SessionFactoryImplementor factory) {
    if (entry.isProcessed()) {
        throw new AssertionFailure("collection was processed twice by flush()");
    }
    entry.setProcessed(true);
    final CollectionPersister loadedPersister = entry.getLoadedPersister();
    final CollectionPersister currentPersister = entry.getCurrentPersister();
    if (loadedPersister != null || currentPersister != null) {
        // it is or was referenced _somewhere_
        // if either its role changed, or its key changed
        final boolean ownerChanged = loadedPersister != currentPersister || !currentPersister.getKeyType().isEqual(entry.getLoadedKey(), entry.getCurrentKey(), factory);
        if (ownerChanged) {
            // do a check
            final boolean orphanDeleteAndRoleChanged = loadedPersister != null && currentPersister != null && loadedPersister.hasOrphanDelete();
            if (orphanDeleteAndRoleChanged) {
                throw new HibernateException("Don't change the reference to a collection with delete-orphan enabled : " + loadedPersister.getRole());
            }
            // do the work
            if (currentPersister != null) {
                entry.setDorecreate(true);
            }
            if (loadedPersister != null) {
                // we will need to remove ye olde entries
                entry.setDoremove(true);
                if (entry.isDorecreate()) {
                    LOG.trace("Forcing collection initialization");
                    collection.forceInitialization();
                }
            }
        } else if (collection.isDirty()) {
            // the collection's elements have changed
            entry.setDoupdate(true);
        }
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) HibernateException(org.hibernate.HibernateException)

Example 37 with AssertionFailure

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

the class TwoPhaseLoad method initializeEntity.

/**
	 * Perform the second step of 2-phase load. Fully initialize the entity
	 * instance.
	 * <p/>
	 * After processing a JDBC result set, we "resolve" all the associations
	 * between the entities which were instantiated and had their state
	 * "hydrated" into an array
	 *
	 * @param entity The entity being loaded
	 * @param readOnly Is the entity being loaded as read-only
	 * @param session The Session
	 * @param preLoadEvent The (re-used) pre-load event
	 */
public static void initializeEntity(final Object entity, final boolean readOnly, final SharedSessionContractImplementor session, final PreLoadEvent preLoadEvent) {
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final EntityEntry entityEntry = persistenceContext.getEntry(entity);
    if (entityEntry == null) {
        throw new AssertionFailure("possible non-threadsafe access to the session");
    }
    doInitializeEntity(entity, entityEntry, readOnly, session, preLoadEvent);
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) AssertionFailure(org.hibernate.AssertionFailure) PersistenceContext(org.hibernate.engine.spi.PersistenceContext)

Example 38 with AssertionFailure

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

the class SequenceStructure method buildCallback.

@Override
public AccessCallback buildCallback(final SharedSessionContractImplementor session) {
    if (sql == null) {
        throw new AssertionFailure("SequenceStyleGenerator's SequenceStructure was not properly initialized");
    }
    return new AccessCallback() {

        @Override
        public IntegralDataTypeHolder getNextValue() {
            accessCounter++;
            try {
                final PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql);
                try {
                    final ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract(st);
                    try {
                        rs.next();
                        final IntegralDataTypeHolder value = IdentifierGeneratorHelper.getIntegralDataTypeHolder(numberType);
                        value.initialize(rs, 1);
                        if (LOG.isDebugEnabled()) {
                            LOG.debugf("Sequence value obtained: %s", value.makeValue());
                        }
                        return value;
                    } finally {
                        try {
                            session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(rs, st);
                        } catch (Throwable ignore) {
                        // intentionally empty
                        }
                    }
                } finally {
                    session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(st);
                    session.getJdbcCoordinator().afterStatementExecution();
                }
            } catch (SQLException sqle) {
                throw session.getJdbcServices().getSqlExceptionHelper().convert(sqle, "could not get next sequence value", sql);
            }
        }

        @Override
        public String getTenantIdentifier() {
            return session.getTenantIdentifier();
        }
    };
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IntegralDataTypeHolder(org.hibernate.id.IntegralDataTypeHolder)

Example 39 with AssertionFailure

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

the class TableStructure method buildCallback.

@Override
public AccessCallback buildCallback(final SharedSessionContractImplementor session) {
    final SqlStatementLogger statementLogger = session.getFactory().getServiceRegistry().getService(JdbcServices.class).getSqlStatementLogger();
    if (selectQuery == null || updateQuery == null) {
        throw new AssertionFailure("SequenceStyleGenerator's TableStructure was not properly initialized");
    }
    final SessionEventListenerManager statsCollector = session.getEventListenerManager();
    return new AccessCallback() {

        @Override
        public IntegralDataTypeHolder getNextValue() {
            return session.getTransactionCoordinator().createIsolationDelegate().delegateWork(new AbstractReturningWork<IntegralDataTypeHolder>() {

                @Override
                public IntegralDataTypeHolder execute(Connection connection) throws SQLException {
                    final IntegralDataTypeHolder value = makeValue();
                    int rows;
                    do {
                        try (PreparedStatement selectStatement = prepareStatement(connection, selectQuery, statementLogger, statsCollector)) {
                            final ResultSet selectRS = executeQuery(selectStatement, statsCollector);
                            if (!selectRS.next()) {
                                final String err = "could not read a hi value - you need to populate the table: " + tableNameText;
                                LOG.error(err);
                                throw new IdentifierGenerationException(err);
                            }
                            value.initialize(selectRS, 1);
                            selectRS.close();
                        } catch (SQLException sqle) {
                            LOG.error("could not read a hi value", sqle);
                            throw sqle;
                        }
                        try (PreparedStatement updatePS = prepareStatement(connection, updateQuery, statementLogger, statsCollector)) {
                            final int increment = applyIncrementSizeToSourceValues ? incrementSize : 1;
                            final IntegralDataTypeHolder updateValue = value.copy().add(increment);
                            updateValue.bind(updatePS, 1);
                            value.bind(updatePS, 2);
                            rows = executeUpdate(updatePS, statsCollector);
                        } catch (SQLException e) {
                            LOG.unableToUpdateQueryHiValue(tableNameText, e);
                            throw e;
                        }
                    } while (rows == 0);
                    accessCounter++;
                    return value;
                }
            }, true);
        }

        @Override
        public String getTenantIdentifier() {
            return session.getTenantIdentifier();
        }
    };
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) SQLException(java.sql.SQLException) Connection(java.sql.Connection) IdentifierGenerationException(org.hibernate.id.IdentifierGenerationException) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) SqlStatementLogger(org.hibernate.engine.jdbc.spi.SqlStatementLogger) SessionEventListenerManager(org.hibernate.engine.spi.SessionEventListenerManager) IntegralDataTypeHolder(org.hibernate.id.IntegralDataTypeHolder)

Example 40 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)

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