use of org.eclipse.persistence.queries.DeleteObjectQuery in project eclipselink by eclipse-ee4j.
the class QueryManagerHasDeleteQueryIsSQLCallQueryTest method setup.
@Override
protected void setup() {
getSession().getIdentityMapAccessor().initializeAllIdentityMaps();
descriptorToModify = project.getDescriptors().get(Employee.class);
DeleteObjectQuery testReadQuery = new DeleteObjectQuery();
SQLCall testCall = new SQLCall();
// setting the SQLCall on ReadObjectQuery
testReadQuery.setCall(testCall);
testReadQuery.setSQLString("testString");
descriptorToModify.getQueryManager().setDeleteQuery(testReadQuery);
}
use of org.eclipse.persistence.queries.DeleteObjectQuery in project eclipselink by eclipse-ee4j.
the class UnitOfWorkImpl method commitToDatabase.
/**
* INTERNAL:
* CommitChanges To The Database from a calculated changeSet
* @param commitTransaction false if called by writeChanges as intent is
* not to finalize the transaction.
*/
protected void commitToDatabase(boolean commitTransaction) {
try {
// CR4202 - ported from 3.6.4
if (wasTransactionBegunPrematurely()) {
// beginTransaction() has been already called
setWasTransactionBegunPrematurely(false);
} else {
beginTransaction();
}
if (commitTransaction) {
setWasNonObjectLevelModifyQueryExecuted(false);
}
this.preDeleteComplete = false;
// PERF: Avoid deletion if nothing to delete.
List deletedObjects = null;
if (hasDeletedObjects()) {
deletedObjects = new ArrayList(this.deletedObjects.size());
for (Object objectToDelete : this.deletedObjects.keySet()) {
ClassDescriptor descriptor = getDescriptor(objectToDelete);
if (descriptor.hasPreDeleteMappings()) {
for (DatabaseMapping mapping : descriptor.getPreDeleteMappings()) {
DeleteObjectQuery deleteQuery = descriptor.getQueryManager().getDeleteQuery();
if (deleteQuery == null) {
deleteQuery = new DeleteObjectQuery();
deleteQuery.setDescriptor(descriptor);
} else {
// Ensure original query has been prepared.
deleteQuery.checkPrepare(this, deleteQuery.getTranslationRow());
deleteQuery = (DeleteObjectQuery) deleteQuery.clone();
}
deleteQuery.setIsExecutionClone(true);
deleteQuery.setTranslationRow(new DatabaseRecord());
deleteQuery.setObject(objectToDelete);
deleteQuery.setSession(this);
mapping.earlyPreDelete(deleteQuery, objectToDelete);
}
}
deletedObjects.add(objectToDelete);
}
this.preDeleteComplete = true;
}
if (this.shouldPerformDeletesFirst) {
if (deletedObjects != null) {
// This must go to the commit manager because uow overrides to do normal deletion.
getCommitManager().deleteAllObjects(deletedObjects);
// Clear change sets of the deleted object to avoid redundant updates.
for (Iterator objects = getObjectsDeletedDuringCommit().keySet().iterator(); objects.hasNext(); ) {
org.eclipse.persistence.internal.sessions.ObjectChangeSet objectChangeSet = (org.eclipse.persistence.internal.sessions.ObjectChangeSet) this.unitOfWorkChangeSet.getObjectChangeSetForClone(objects.next());
if (objectChangeSet != null) {
objectChangeSet.clear(true);
}
}
}
// Let the commit manager figure out how to write the objects
super.writeAllObjectsWithChangeSet(this.unitOfWorkChangeSet);
// Issue all the SQL for the ModifyAllQuery's, don't touch the cache though
issueModifyAllQueryList();
} else {
// Let the commit manager figure out how to write the objects
super.writeAllObjectsWithChangeSet(this.unitOfWorkChangeSet);
if (deletedObjects != null) {
// This must go to the commit manager because uow overrides to do normal deletion.
getCommitManager().deleteAllObjects(deletedObjects);
}
// Issue all the SQL for the ModifyAllQuery's, don't touch the cache though
issueModifyAllQueryList();
}
// Issue prepare event.
if (this.eventManager != null) {
this.eventManager.prepareUnitOfWork();
}
// do not lock objects unless we are at the commit stage
if (commitTransaction) {
try {
acquireWriteLocks();
commitTransaction();
} catch (RuntimeException throwable) {
releaseWriteLocks();
throw throwable;
} catch (Error throwable) {
releaseWriteLocks();
throw throwable;
}
} else {
setWasTransactionBegunPrematurely(true);
// must let the UnitOfWork know that the transaction was begun
// before the commit process.
}
} catch (RuntimeException exception) {
// The number of SQL statements been prepared need be stored into UOW
// before any exception being thrown.
copyStatementsCountIntoProperties();
try {
rollbackTransaction(commitTransaction);
} catch (RuntimeException ignore) {
// Ignore
}
if (hasExceptionHandler()) {
getExceptionHandler().handleException(exception);
} else {
throw exception;
}
}
}
use of org.eclipse.persistence.queries.DeleteObjectQuery in project eclipselink by eclipse-ee4j.
the class EISOneToManyMapping method preDelete.
/**
* INTERNAL:
* Delete the reference objects.
*/
@Override
public void preDelete(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException {
if (isForeignKeyRelationship()) {
return;
}
if (!this.shouldObjectModifyCascadeToParts(query)) {
return;
}
Object objects = this.getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession());
ContainerPolicy cp = this.getContainerPolicy();
// else delete everything in one shot
if (this.mustDeleteReferenceObjectsOneByOne()) {
for (Object iter = cp.iteratorFor(objects); cp.hasNext(iter); ) {
DeleteObjectQuery deleteQuery = new DeleteObjectQuery();
deleteQuery.setIsExecutionClone(true);
deleteQuery.setObject(cp.next(iter, query.getSession()));
deleteQuery.setCascadePolicy(query.getCascadePolicy());
query.getSession().executeQuery(deleteQuery);
}
if (!query.getSession().isUnitOfWork()) {
// This deletes any objects on the database, as the collection in memory may have been changed.
// This is not required for unit of work, as the update would have already deleted these objects,
// and the backup copy will include the same objects causing double deletes.
this.deleteReferenceObjectsLeftOnDatabase(query);
}
} else {
this.deleteAll(query);
}
}
use of org.eclipse.persistence.queries.DeleteObjectQuery in project eclipselink by eclipse-ee4j.
the class EISOneToManyMapping method postDelete.
/**
* INTERNAL:
* Delete the reference objects.
*/
@Override
public void postDelete(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException {
if (!isForeignKeyRelationship()) {
return;
}
if (!this.shouldObjectModifyCascadeToParts(query)) {
return;
}
Object referenceObjects = this.getRealCollectionAttributeValueFromObject(query.getObject(), query.getSession());
// otherwise, delete the reference objects one by one
if (this.hasCustomDeleteAllQuery()) {
this.deleteAll(query, referenceObjects);
} else {
ContainerPolicy cp = this.getContainerPolicy();
for (Object iter = cp.iteratorFor(referenceObjects); cp.hasNext(iter); ) {
DeleteObjectQuery deleteQuery = new DeleteObjectQuery();
deleteQuery.setIsExecutionClone(true);
deleteQuery.setObject(cp.next(iter, query.getSession()));
deleteQuery.setCascadePolicy(query.getCascadePolicy());
query.getSession().executeQuery(deleteQuery);
}
if (!query.getSession().isUnitOfWork()) {
// This deletes any objects on the database, as the collection in memory may have been changed.
// This is not required for unit of work, as the update would have already deleted these objects,
// and the backup copy will include the same objects, causing double deletes.
this.deleteReferenceObjectsLeftOnDatabase(query);
}
}
}
use of org.eclipse.persistence.queries.DeleteObjectQuery in project eclipselink by eclipse-ee4j.
the class CommitManager method deleteAllObjects.
/**
* Delete all of the objects with the matching class.
*/
public void deleteAllObjects(Class<?> theClass, List objects, AbstractSession session) {
ClassDescriptor descriptor = null;
if (((UnitOfWorkImpl) session).getCommitOrder() != CommitOrderType.NONE) {
// bug 331064 - Sort the delete order
objects = sort(theClass, objects);
}
int size = objects.size();
for (int index = 0; index < size; index++) {
Object objectToDelete = objects.get(index);
if (objectToDelete.getClass() == theClass) {
if (descriptor == null) {
descriptor = session.getDescriptor(theClass);
}
// PERF: Get the descriptor query, to avoid extra query creation.
DeleteObjectQuery deleteQuery = descriptor.getQueryManager().getDeleteQuery();
if (deleteQuery == null) {
deleteQuery = new DeleteObjectQuery();
deleteQuery.setDescriptor(descriptor);
} else {
// Ensure original query has been prepared.
deleteQuery.checkPrepare(session, deleteQuery.getTranslationRow());
deleteQuery = (DeleteObjectQuery) deleteQuery.clone();
}
deleteQuery.setIsExecutionClone(true);
deleteQuery.setObject(objectToDelete);
session.executeQuery(deleteQuery);
}
}
}
Aggregations