use of org.eclipse.persistence.queries.UpdateAllQuery 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.UpdateAllQuery 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.UpdateAllQuery in project eclipselink by eclipse-ee4j.
the class NestedUOWWithUpdateAllQueryTest method test.
@Override
public void test() {
UnitOfWork uow1 = getSession().acquireUnitOfWork();
UnitOfWork uow2 = uow1.acquireUnitOfWork();
ExpressionBuilder eb = new ExpressionBuilder();
UpdateAllQuery updateQuery = new UpdateAllQuery(Employee.class);
updateQuery.addUpdate(eb.get("lastName"), "dummyLastName");
try {
uow2.executeQuery(updateQuery);
} catch (org.eclipse.persistence.exceptions.EclipseLinkException e) {
caughtException = e;
}
}
use of org.eclipse.persistence.queries.UpdateAllQuery in project eclipselink by eclipse-ee4j.
the class UpdateAllQueryTestHelper method execute.
protected static String execute(Session mainSession, UpdateAllQuery uq, boolean handleChildren, Class<?> rootClass) {
String errorMsg = "";
ClassDescriptor descriptor = mainSession.getDescriptor(uq.getReferenceClass());
clearCache(mainSession);
// original objects
Vector objects = mainSession.readAllObjects(rootClass);
// first update using the original TopLink approach - one by one.
// That will be done using report query - it will use the same selection criteria
// as UpdateAllQuery and each attribute will correspond to an update item.
ReportQuery rq = new ReportQuery(uq.getReferenceClass(), uq.getExpressionBuilder());
rq.setSelectionCriteria(uq.getSelectionCriteria());
rq.setShouldRetrievePrimaryKeys(true);
// some db platforms don't allow nulls in select clause - so add the fields with null values to the query result.
Vector fieldsWithNullValues = new Vector();
Iterator itEntrySets = uq.getUpdateClauses().entrySet().iterator();
while (itEntrySets.hasNext()) {
Map.Entry entry = (Map.Entry) itEntrySets.next();
Expression valueExpression;
String keyString = getQualifiedFieldNameFromKey(entry.getKey(), rq.getReferenceClass(), descriptor, mainSession);
Object value = entry.getValue();
DatabaseMapping mapping = descriptor.getObjectBuilder().getMappingForField(new DatabaseField(keyString));
if (mapping != null && mapping.isOneToOneMapping() && value != null) {
// Note that this only works in case the reference PK is not compound
if (((OneToOneMapping) mapping).getSourceToTargetKeyFields().size() > 1) {
errorMsg = "Attribute " + mapping.getAttributeName() + " mapped with 1to1 mapping that has more than one targetKeyField. UpdateAllQueryTestHelper currently doesn't support that.";
}
DatabaseField targetField = ((OneToOneMapping) mapping).getSourceToTargetKeyFields().get(new DatabaseField(keyString));
if (value instanceof Expression) {
valueExpression = ((Expression) (((Expression) value).clone())).getField(targetField);
} else {
ClassDescriptor targetDescriptor = mapping.getReferenceDescriptor();
Object fieldValue = targetDescriptor.getObjectBuilder().extractValueFromObjectForField(value, targetField, (org.eclipse.persistence.internal.sessions.AbstractSession) mainSession);
valueExpression = rq.getExpressionBuilder().value(fieldValue);
}
} else {
if (value instanceof Expression) {
valueExpression = (Expression) value;
} else {
valueExpression = rq.getExpressionBuilder().value(value);
}
}
if (value == null) {
fieldsWithNullValues.add(keyString);
} else {
rq.addAttribute(keyString, valueExpression);
}
}
UnitOfWork uow = mainSession.acquireUnitOfWork();
// mainSession could be a ServerSession
AbstractSession session = uow.getParent();
// report query results contain the values to be assigned for each object to be updated.
Vector result = (Vector) session.executeQuery(rq);
Vector objectsAfterOneByOneUpdate = new Vector(objects.size());
session.beginTransaction();
try {
for (int i = 0; i < result.size(); i++) {
// read through uow the object(clone) to be updated
ReportQueryResult reportResult = (ReportQueryResult) result.elementAt(i);
// hammer into the object the updated values
Object obj = reportResult.readObject(rq.getReferenceClass(), uow);
DatabaseRecord row = new DatabaseRecord();
for (int j = 0; j < reportResult.getNames().size(); j++) {
String name = reportResult.getNames().get(j);
DatabaseField field = new DatabaseField(name);
Object value = reportResult.getResults().get(j);
row.add(field, value);
}
// some db platforms don't allow nulls in select clause - so add the fields with null values to the query result
for (int j = 0; j < fieldsWithNullValues.size(); j++) {
String name = (String) fieldsWithNullValues.elementAt(j);
DatabaseField field = new DatabaseField(name);
row.add(field, null);
}
rq.getDescriptor().getObjectBuilder().assignReturnRow(obj, (AbstractSession) uow, row, null);
}
// uow committed - objects updated.
uow.commit();
// objects are copied into another vector - later it will be compared with UpdateAllQuery result.
for (int i = 0; i < objects.size(); i++) {
Object original = objects.elementAt(i);
Object copy = buildCopy(descriptor, original, uow);
objectsAfterOneByOneUpdate.add(copy);
}
} finally {
// transaction rolled back - objects back to the original state in the db.
session.rollbackTransaction();
}
clearCache(mainSession);
// now use UpdateAllQuery
uow = mainSession.acquireUnitOfWork();
// mainSession could be a ServerSession
session = uow.getParent();
Vector objectsAfterUpdateAll = new Vector(objects.size());
session.beginTransaction();
try {
uow.executeQuery(uq);
// uow committed - objects updated.
uow.commit();
// objects are copied into another vector - it will be compared with update one-by-one result.
for (int i = 0; i < objects.size(); i++) {
Object original = objects.elementAt(i);
Object copy = buildCopy(descriptor, original, uow);
objectsAfterUpdateAll.add(copy);
}
} finally {
// transaction rolled back - objects back to the original state in the db.
session.rollbackTransaction();
}
clearCache(mainSession);
// verify
String classErrorMsg = "";
for (int i = 0; i < objects.size(); i++) {
Object obj = objects.elementAt(i);
Object obj1 = objectsAfterOneByOneUpdate.elementAt(i);
Object obj2 = objectsAfterUpdateAll.elementAt(i);
boolean equal = rq.getDescriptor().getObjectBuilder().compareObjects(obj, obj2, session);
if (!equal) {
classErrorMsg = classErrorMsg + "Difference: original = " + obj.toString() + "; afterOneByOneUpdate = " + obj1.toString() + "; afterUpdateAll = " + obj2.toString() + ";";
}
}
if (classErrorMsg.length() > 0) {
errorMsg = errorMsg + 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();
UpdateAllQuery childUq = (UpdateAllQuery) uq.clone();
childUq.setReferenceClass(childReferenceClass);
childUq.setIsPrepared(false);
errorMsg += execute(mainSession, childUq, handleChildren, rootClass);
}
}
}
return errorMsg;
}
use of org.eclipse.persistence.queries.UpdateAllQuery in project eclipselink by eclipse-ee4j.
the class UpdateAllQueryTestHelper method createUpdateAllQuery.
public static UpdateAllQuery createUpdateAllQuery(Class<?> referenceClass, HashMap updateClauses, Expression selectionExpression) {
// Construct UpdateAllQuery
UpdateAllQuery uq = new UpdateAllQuery(referenceClass, selectionExpression);
Iterator itEntrySets = updateClauses.entrySet().iterator();
while (itEntrySets.hasNext()) {
Map.Entry entry = (Map.Entry) itEntrySets.next();
uq.addUpdate((String) entry.getKey(), entry.getValue());
}
return uq;
}
Aggregations