org.eclipse.persistence.internal.jpa.querydef.CriteriaUpdateImpl Maven / Gradle / Ivy
Show all versions of eclipselink Show documentation
/*
* Copyright (c) 2011, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// 10/26/2012-2.5 Chris Delahunt
// - 350469: JPA 2.1 Criteria Query framework Bulk Update/Delete support
package org.eclipse.persistence.internal.jpa.querydef;
import javax.persistence.criteria.CriteriaUpdate;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.metamodel.SingularAttribute;
import org.eclipse.persistence.queries.DatabaseQuery;
import org.eclipse.persistence.queries.UpdateAllQuery;
/**
*
* Purpose: Contains the implementation of the CriteriaUpdate interface of
* the JPA criteria API.
*
* Description: This is the container class for the components that
* define an Update Query.
*
*
* @see javax.persistence.criteria CriteriaUpdate
*
* @author Chris Delahunt
* @since EclipseLink 2.5
*/
public class CriteriaUpdateImpl extends CommonAbstractCriteriaImpl implements CriteriaUpdate {
private static final long serialVersionUID = 5069513243268181150L;
protected Root root;
protected UpdateAllQuery query;
public CriteriaUpdateImpl(Metamodel metamodel, CriteriaBuilderImpl queryBuilder, Class resultType){
super(metamodel, queryBuilder, resultType);
//initialize the query.
query = new UpdateAllQuery(this.queryType);
query.setShouldDeferExecutionInUOW(false);
}
@Override
public Root from(Class entityClass) {
return getRoot();
}
@Override
public Root from(EntityType entity) {
return getRoot();
}
@Override
public Root getRoot() {
if (this.root == null) {
if (getResultType() !=null) {
EntityType entity = this.metamodel.entity(this.queryType);
RootImpl newRoot = new RootImpl(entity, this.metamodel, this.queryType, query.getExpressionBuilder(), entity);
this.root = newRoot;
}
}
return this.root;
}
@Override
public CriteriaUpdate set(
SingularAttribute super T, Y> attribute, X value) {
if(value instanceof Expression) {
findRootAndParameters((Expression)value);
}
query.addUpdate(attribute.getName(), value);
return this;
}
@Override
public CriteriaUpdate set(SingularAttribute super T, Y> attribute,
Expression extends Y> value) {
findRootAndParameters(value);
query.addUpdate(attribute.getName(), ((InternalSelection)value).getCurrentNode());
return this;
}
@Override
public CriteriaUpdate set(Path attribute, X value) {
if(value instanceof Expression) {
findRootAndParameters((Expression)value);
}
query.addUpdate(((PathImpl)attribute).getCurrentNode(), value);
return this;
}
@Override
public CriteriaUpdate set(Path attribute,
Expression extends Y> value) {
findRootAndParameters(value);
query.addUpdate(((PathImpl)attribute).getCurrentNode(), ((InternalSelection)value).getCurrentNode());
return this;
}
@Override
public CriteriaUpdate set(String attributeName, Object value) {
if(value instanceof Expression) {
findRootAndParameters((Expression)value);
value = ((InternalSelection)value).getCurrentNode();
}
query.addUpdate(attributeName, value);
return this;
}
@Override
public CriteriaUpdate where(Expression restriction) {
return (CriteriaUpdate)super.where(restriction);
}
@Override
public CriteriaUpdate where(Predicate... restrictions) {
return (CriteriaUpdate)super.where(restrictions);
}
@Override
protected void integrateRoot(RootImpl root) {
if (this.root != root) {
this.root = root;
}
}
@Override
protected org.eclipse.persistence.expressions.Expression getBaseExpression() {
return query.getExpressionBuilder();
}
@Override
protected DatabaseQuery getDatabaseQuery() {
return (DatabaseQuery)query.clone();
}
}