use of org.eclipse.persistence.queries.DeleteAllQuery in project eclipselink by eclipse-ee4j.
the class JPQLParseTree method populateQuery.
/**
* Add all of the relevant query settings from an EJBQLParseTree to the given
* database query.
* @param query The query to populate
* @param session The session to use to information such as descriptors.
*/
public void populateQuery(DatabaseQuery query, AbstractSession session) {
if (query.isObjectLevelReadQuery()) {
ObjectLevelReadQuery objectQuery = (ObjectLevelReadQuery) query;
GenerationContext generationContext = buildContext(objectQuery, session);
populateReadQueryInternal(objectQuery, generationContext);
} else if (query.isUpdateAllQuery()) {
UpdateAllQuery updateQuery = (UpdateAllQuery) query;
GenerationContext generationContext = buildContext(updateQuery, session);
populateModifyQueryInternal(updateQuery, generationContext);
addUpdatesToQuery(updateQuery, generationContext);
} else if (query.isDeleteAllQuery()) {
DeleteAllQuery deleteQuery = (DeleteAllQuery) query;
GenerationContext generationContext = buildContext(deleteQuery, session);
populateModifyQueryInternal(deleteQuery, generationContext);
}
}
use of org.eclipse.persistence.queries.DeleteAllQuery in project eclipselink by eclipse-ee4j.
the class WriteChanges_DeleteAll_TestCase method test.
@Override
public void test() {
UnitOfWork uow = getSession().acquireUnitOfWork();
try {
Vector employees = uow.readAllObjects(Employee.class);
uow.writeChanges();
DeleteAllQuery deleteAllQuery = new DeleteAllQuery();
deleteAllQuery.setReferenceClass(Employee.class);
deleteAllQuery.setObjects(employees);
uow.executeQuery(deleteAllQuery);
} catch (Exception e) {
exception = e;
} finally {
uow.release();
}
}
use of org.eclipse.persistence.queries.DeleteAllQuery in project eclipselink by eclipse-ee4j.
the class STI_JoinedAttributeTest method clear.
protected void clear() {
UnitOfWork uow = getSession().acquireUnitOfWork();
UpdateAllQuery updateEmployees = new UpdateAllQuery(STI_Employee.class);
updateEmployees.addUpdate("manager", null);
uow.executeQuery(updateEmployees);
UpdateAllQuery updateProjects = new UpdateAllQuery(STI_Project.class);
updateProjects.addUpdate("teamLeader", null);
uow.executeQuery(updateProjects);
uow.executeQuery(new DeleteAllQuery(STI_Employee.class));
uow.executeQuery(new DeleteAllQuery(STI_Project.class));
uow.commit();
clearCache();
}
use of org.eclipse.persistence.queries.DeleteAllQuery in project eclipselink by eclipse-ee4j.
the class DeleteAllQueryTestHelper method execute.
protected static String execute(Session mainSession, Class<?> referenceClass, Expression selectionExpression, boolean shouldDeferExecutionInUOW, boolean handleChildren, Class<?> rootClass) {
String errorMsg = "";
clearCache(mainSession);
// first delete using the original TopLink approach - one by one.
UnitOfWork uow = mainSession.acquireUnitOfWork();
// mainSession could be a ServerSession
AbstractSession session = uow.getParent();
// Will need to bring the db back to its original state
// so that comparison of the deletion result would be possible.
session.beginTransaction();
Vector objectsToDelete = uow.readAllObjects(referenceClass, selectionExpression);
ClassDescriptor descriptor = mainSession.getClassDescriptor(referenceClass);
uow.deleteAllObjects(objectsToDelete);
mainSession.logMessage("***delete one by one");
uow.commit();
Vector objectsLeftAfterOriginalDeletion = session.readAllObjects(rootClass);
session.rollbackTransaction();
// now delete using DeleteAllQuery.
clearCache(mainSession);
// bring all objects into cache
session.readAllObjects(rootClass);
uow = mainSession.acquireUnitOfWork();
// mainSession could be a ServerSession
session = uow.getParent();
// Will need to bring the db back to its original state
// so that the in case thre are children descriptors
// they would still have objects to work with.
session.beginTransaction();
DeleteAllQuery query = new DeleteAllQuery(referenceClass, selectionExpression);
query.setShouldDeferExecutionInUOW(shouldDeferExecutionInUOW);
uow.executeQuery(query);
mainSession.logMessage("***DeleteAllQuery for class " + referenceClass.getName());
uow.commit();
// verify that cache invalidation worked correctly:
// deleted objects should've disappeared, others remain
String classErrorMsg = "";
for (int i = 0; i < objectsToDelete.size(); i++) {
Object deletedObject = session.readObject(objectsToDelete.elementAt(i));
if (deletedObject != null) {
classErrorMsg = classErrorMsg + "Deleted object " + deletedObject + " is stil in cache; ";
break;
}
}
for (int i = 0; i < objectsLeftAfterOriginalDeletion.size(); i++) {
Object remainingObject = objectsLeftAfterOriginalDeletion.elementAt(i);
Object remainingObjectRead = session.readObject(remainingObject);
if (remainingObjectRead == null) {
classErrorMsg = classErrorMsg + "Remaining object " + remainingObject + " is not in cache; ";
break;
}
}
// now let's verify that the objects were correctly deleted from the db
clearCache(mainSession);
// deleted objects should've disappeared, others remain
for (int i = 0; i < objectsToDelete.size(); i++) {
Object deletedObject = session.readObject(objectsToDelete.elementAt(i));
if (deletedObject != null) {
classErrorMsg = classErrorMsg + "Deleted object " + deletedObject + " is stil in db; ";
break;
}
}
for (int i = 0; i < objectsLeftAfterOriginalDeletion.size(); i++) {
Object remainingObject = objectsLeftAfterOriginalDeletion.elementAt(i);
Object remainingObjectRead = session.readObject(remainingObject);
if (remainingObjectRead == null) {
classErrorMsg = classErrorMsg + "Remaining object " + remainingObject + " is not in db; ";
break;
}
}
session.rollbackTransaction();
if (classErrorMsg.length() > 0) {
String className = referenceClass.getName();
String shortClassName = className.substring(className.lastIndexOf('.') + 1);
errorMsg = errorMsg + " " + shortClassName + ": " + classErrorMsg;
}
if (handleChildren) {
if (descriptor.hasInheritance() && descriptor.getInheritancePolicy().hasChildren()) {
Iterator<ClassDescriptor> it = descriptor.getInheritancePolicy().getChildDescriptors().iterator();
while (it.hasNext()) {
ClassDescriptor childDescriptor = it.next();
Class<?> childReferenceClass = childDescriptor.getJavaClass();
errorMsg += execute(mainSession, childReferenceClass, selectionExpression, shouldDeferExecutionInUOW, handleChildren, rootClass);
}
}
}
return errorMsg;
}
use of org.eclipse.persistence.queries.DeleteAllQuery in project eclipselink by eclipse-ee4j.
the class OneToManyMapping method setDeleteAllSQLString.
/**
* PUBLIC:
* Set the SQL string used by the mapping to delete the target objects.
* This allows the developer to override the SQL
* generated by TopLink with a custom SQL statement or procedure call.
* The arguments are
* translated from the fields of the source row, by replacing the field names
* marked by '#' with the values for those fields at execution time.
* A one-to-many mapping will only use this delete all optimization if the target objects
* can be deleted in a single SQL call. This is possible when the target objects
* are in a single table, do not using locking, do not contain other privately-owned
* parts, do not read subclasses, etc.
* <p>
* Example: "delete from PHONE where OWNER_ID = #EMPLOYEE_ID"
*/
@Override
public void setDeleteAllSQLString(String sqlString) {
DeleteAllQuery query = new DeleteAllQuery();
query.setSQLString(sqlString);
setCustomDeleteAllQuery(query);
}
Aggregations