Skip to content

Commit

Permalink
Revert "removed createCriteria calls"
Browse files Browse the repository at this point in the history
This reverts commit fb912e2
  • Loading branch information
1azyman committed Feb 23, 2018
1 parent 43ca530 commit 11d5abb
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 85 deletions.
Expand Up @@ -43,15 +43,14 @@
import org.hibernate.FlushMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.pagination.LimitHandler;
import org.hibernate.engine.spi.RowSelection;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
import java.sql.Statement;
Expand Down Expand Up @@ -576,10 +575,9 @@ private int selectRecordsByMaxAge(Session session, String tempTable, Date minVal
}

private int selectRecordsByNumberToKeep(Session session, String tempTable, Integer recordsToKeep, Dialect dialect) {
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(RAuditEventRecord.class);
cq.select(cb.count(cq.from(RAuditEventRecord.class)));
Number totalAuditRecords = (Number) session.createQuery(cq).uniqueResult();
Number totalAuditRecords = (Number) session.createCriteria(RAuditEventRecord.class)
.setProjection(Projections.rowCount())
.uniqueResult();
int recordsToDelete = totalAuditRecords.intValue() - recordsToKeep;
if (recordsToDelete <= 0) {
recordsToDelete = 0;
Expand Down
Expand Up @@ -58,8 +58,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import java.util.*;

/**
Expand Down Expand Up @@ -403,15 +401,12 @@ public <T extends ObjectType> void updateLoadedCampaign(PrismObject<T> object,

LOGGER.debug("Loading certification campaign cases.");

CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(RAccessCertificationCase.class);
cq.where(cb.equal(cq.from(RAccessCertificationCase.class).get("ownerOid"), object.getOid()));

Query query = session.createQuery(cq);
Criteria criteria = session.createCriteria(RAccessCertificationCase.class);
criteria.add(Restrictions.eq("ownerOid", object.getOid()));

// TODO fetch only XML representation
@SuppressWarnings({"raw", "unchecked"})
List<RAccessCertificationCase> cases = query.list();
List<RAccessCertificationCase> cases = criteria.list();
if (cases == null || cases.isEmpty()) {
return;
}
Expand Down
Expand Up @@ -41,14 +41,14 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.Criteria;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -212,19 +212,38 @@ public <T extends ObjectType> void updateLoadedLookupTable(PrismObject<T> object

GetOperationOptions getOption = findLookupTableGetOption(options);
RelationalValueSearchQuery queryDef = getOption == null ? null : getOption.getRelationalValueSearchQuery();
Query query = setupLookupTableRowsQuery(session, queryDef, object.getOid());
Criteria criteria = setupLookupTableRowsQuery(session, queryDef, object.getOid());
if (queryDef != null && queryDef.getPaging() != null) {
ObjectPaging paging = queryDef.getPaging();

if (paging.getOffset() != null) {
query.setFirstResult(paging.getOffset());
criteria.setFirstResult(paging.getOffset());
}
if (paging.getMaxSize() != null) {
query.setMaxResults(paging.getMaxSize());
criteria.setMaxResults(paging.getMaxSize());
}

ItemPath orderByPath = paging.getOrderBy();
if (paging.getDirection() != null && orderByPath != null && !orderByPath.isEmpty()) {
if (orderByPath.size() > 1 ||
!(orderByPath.first() instanceof NameItemPathSegment) && !(orderByPath.first() instanceof IdentifierPathSegment)) {
throw new SchemaException("OrderBy has to consist of just one naming or identifier segment");
}
ItemPathSegment first = orderByPath.first();
String orderBy = first instanceof NameItemPathSegment ?
((NameItemPathSegment) first).getName().getLocalPart() : RLookupTableRow.ID_COLUMN_NAME;
switch (paging.getDirection()) {
case ASCENDING:
criteria.addOrder(Order.asc(orderBy));
break;
case DESCENDING:
criteria.addOrder(Order.desc(orderBy));
break;
}
}
}

List<RLookupTableRow> rows = query.list();
List<RLookupTableRow> rows = criteria.list();
if (rows == null || rows.isEmpty()) {
return;
}
Expand All @@ -237,19 +256,12 @@ public <T extends ObjectType> void updateLoadedLookupTable(PrismObject<T> object
}
}

private Query setupLookupTableRowsQuery(Session session, RelationalValueSearchQuery queryDef, String oid) throws SchemaException {
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(RLookupTableRow.class);

Root<RLookupTableRow> root = cq.from(RLookupTableRow.class);

cq.where(cb.equal(root.get("ownerOid"), oid));

if (queryDef == null) {
return session.createQuery(cq);
}
private Criteria setupLookupTableRowsQuery(Session session, RelationalValueSearchQuery queryDef, String oid) {
Criteria criteria = session.createCriteria(RLookupTableRow.class);
criteria.add(Restrictions.eq("ownerOid", oid));

if (queryDef.getColumn() != null
if (queryDef != null
&& queryDef.getColumn() != null
&& queryDef.getSearchType() != null
&& StringUtils.isNotEmpty(queryDef.getSearchValue())) {

Expand All @@ -264,43 +276,17 @@ private Query setupLookupTableRowsQuery(Session session, RelationalValueSearchQu
}
switch (queryDef.getSearchType()) {
case EXACT:
cq.where(cb.equal(root.get(param), value));
criteria.add(Restrictions.eq(param, value));
break;
case STARTS_WITH:
cq.where(cb.like(root.get(param), value + "%"));
criteria.add(Restrictions.like(param, value + "%"));
break;
case SUBSTRING:
cq.where(cb.like(root.get(param), "%" + value + "%"));
}
}

ObjectPaging paging = queryDef.getPaging();
if (paging == null) {
return session.createQuery(cq);
}

ItemPath orderByPath = paging.getOrderBy();
if (paging.getDirection() != null && orderByPath != null && !orderByPath.isEmpty()) {
if (orderByPath.size() > 1 ||
!(orderByPath.first() instanceof NameItemPathSegment) && !(orderByPath.first() instanceof IdentifierPathSegment)) {
throw new SchemaException("OrderBy has to consist of just one naming or identifier segment");
}

ItemPathSegment first = orderByPath.first();
String orderBy = first instanceof NameItemPathSegment ?
((NameItemPathSegment) first).getName().getLocalPart() : RLookupTableRow.ID_COLUMN_NAME;

switch (paging.getDirection()) {
case ASCENDING:
cq.orderBy(cb.asc(root.get(orderBy)));
break;
case DESCENDING:
cq.orderBy(cb.desc(root.get(orderBy)));
break;
criteria.add(Restrictions.like(param, "%" + value + "%"));
}
}

return session.createQuery(cq);
return criteria;
}

public <T extends ObjectType> Collection<? extends ItemDelta> filterLookupTableModifications(Class<T> type,
Expand Down
Expand Up @@ -49,15 +49,14 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
import org.hibernate.*;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.NativeQuery;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.xml.namespace.QName;
import java.util.*;

Expand Down Expand Up @@ -172,16 +171,11 @@ public <T extends ObjectType> PrismObject<T> getObjectInternal(Session session,
// this just loads object to hibernate session, probably will be removed later. Merge after this get
// will be faster. Read and use object only from fullObject column.
// todo remove this later [lazyman]
Class clazz = ClassMapper.getHQLTypeClass(type);

CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(clazz);
cq.where(cb.equal(cq.from(clazz).get("oid"), oid));

Query query = session.createQuery(cq);
query.setLockOptions(lockOptions);
Criteria criteria = session.createCriteria(ClassMapper.getHQLTypeClass(type));
criteria.add(Restrictions.eq("oid", oid));

RObject obj = (RObject) query.uniqueResult();
criteria.setLockMode(lockOptions.getLockMode());
RObject obj = (RObject) criteria.uniqueResult();

if (obj != null) {
fullObject = new GetObjectResult(obj.getOid(), obj.getFullObject(), obj.getStringsCount(), obj.getLongsCount(),
Expand Down
Expand Up @@ -52,17 +52,17 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.query.NativeQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.persistence.PersistenceException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.*;
Expand Down Expand Up @@ -332,13 +332,8 @@ public <T extends ObjectType> Object deleteObjectAttempt(Class<T> type, String o

closureContext = closureManager.onBeginTransactionDelete(session, type, oid);

Class clazz = ClassMapper.getHQLTypeClass(type);

CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(clazz);
cq.where(cb.equal(cq.from(clazz).get("oid"), oid));

Query query = session.createQuery(cq);
Criteria query = session.createCriteria(ClassMapper.getHQLTypeClass(type));
query.add(Restrictions.eq("oid", oid));
RObject object = (RObject) query.uniqueResult();
if (object == null) {
throw new ObjectNotFoundException("Object of type '" + type.getSimpleName() + "' with oid '" + oid
Expand Down

0 comments on commit 11d5abb

Please sign in to comment.