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);
}
}
}
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);
}
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();
}
};
}
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();
}
};
}
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);
}
}
Aggregations