use of org.hibernate.query.Query in project dhis2-core by dhis2.
the class HibernateGenericStore method getAllByAttributes.
@Override
public List<T> getAllByAttributes(List<Attribute> attributes) {
CriteriaBuilder builder = getCriteriaBuilder();
CriteriaQuery<T> query = builder.createQuery(getClazz());
Root<T> root = query.from(getClazz());
query.select(root).distinct(true);
List<Predicate> predicates = attributes.stream().map(attribute -> builder.isNotNull(builder.function(FUNCTION_JSONB_EXTRACT_PATH, String.class, root.get("attributeValues"), builder.literal(attribute.getUid())))).collect(Collectors.toList());
query.where(builder.or(predicates.toArray(new Predicate[predicates.size()])));
return getSession().createQuery(query).list();
}
use of org.hibernate.query.Query in project dhis2-core by dhis2.
the class HibernateGenericStore method countAllValuesByAttributes.
@Override
public long countAllValuesByAttributes(List<Attribute> attributes) {
CriteriaBuilder builder = getCriteriaBuilder();
CriteriaQuery<Long> query = builder.createQuery(Long.class);
Root<T> root = query.from(getClazz());
query.select(builder.countDistinct(root));
List<Predicate> predicates = attributes.stream().map(attribute -> builder.isNotNull(builder.function(FUNCTION_JSONB_EXTRACT_PATH, String.class, root.get("attributeValues"), builder.literal(attribute.getUid())))).collect(Collectors.toList());
query.where(builder.or(predicates.toArray(new Predicate[predicates.size()])));
return getSession().createQuery(query).getSingleResult();
}
use of org.hibernate.query.Query in project midpoint by Evolveum.
the class AbstractOrgClosureTest method _test110ScanOrgStructure.
protected void _test110ScanOrgStructure() throws Exception {
OperationResult opResult = new OperationResult("===[ test110ScanOrgStructure ]===");
long start = System.currentTimeMillis();
scanOrgStructure(opResult);
System.out.println("Found " + allOrgCreated.size() + " orgs and " + (objectCount - allOrgCreated.size()) + " users in " + (System.currentTimeMillis() - start) + " ms");
try (Session session = openSession()) {
Query q = session.createNativeQuery("select count(*) from m_org_closure");
System.out.println("OrgClosure table has " + q.list().get(0) + " rows");
closureSize = Long.parseLong(q.list().get(0).toString());
}
}
use of org.hibernate.query.Query in project midpoint by Evolveum.
the class AbstractOrgClosureTest method checkClosureMatrix.
protected boolean checkClosureMatrix() throws SchemaException {
try (Session session = openSession()) {
// we compute the closure table "by hand" as 1 + A + A^2 + A^3 + ... + A^n where n is the greatest expected path length
int vertices = getVertices().size();
long start = System.currentTimeMillis();
// used to give indices to vertices
List<String> vertexList = new ArrayList<>(getVertices());
if (DUMP_TC_MATRIX_DETAILS) {
logger.info("Vertex list = {}", vertexList);
}
DoubleMatrix2D a = new SparseDoubleMatrix2D(vertices, vertices);
// }
for (DefaultEdge edge : orgGraph.edgeSet()) {
a.set(vertexList.indexOf(orgGraph.getEdgeSource(edge)), vertexList.indexOf(orgGraph.getEdgeTarget(edge)), 1.0);
}
DoubleMatrix2D result = new SparseDoubleMatrix2D(vertices, vertices);
for (int i = 0; i < vertices; i++) {
result.setQuick(i, i, 1.0);
}
DoubleMatrix2D power = result.copy();
Algebra alg = new Algebra();
for (int level = 1; level <= maxLevel; level++) {
power = alg.mult(power, a);
result.assign(power, Functions.plus);
// System.out.println("a=" + a);
// System.out.println("a^"+level+"="+power);
}
logger.info("TC matrix computed in {} ms", System.currentTimeMillis() - start);
if (DUMP_TC_MATRIX_DETAILS) {
logger.info("TC matrix expected = {}", result);
}
Query q = session.createNativeQuery("select descendant_oid, ancestor_oid, val from m_org_closure").addScalar("descendant_oid", StringType.INSTANCE).addScalar("ancestor_oid", StringType.INSTANCE).addScalar("val", LongType.INSTANCE);
List<Object[]> list = q.list();
logger.info("OrgClosure has {} rows", list.size());
DoubleMatrix2D closureInDatabase = new SparseDoubleMatrix2D(vertices, vertices);
for (Object[] item : list) {
int val = Integer.parseInt(item[2].toString());
if (val == 0) {
throw new IllegalStateException("Row with val == 0 in closure table: " + list);
}
closureInDatabase.set(vertexList.indexOf(item[0]), vertexList.indexOf(item[1]), val);
}
if (DUMP_TC_MATRIX_DETAILS) {
logger.info("TC matrix fetched from db = {}", closureInDatabase);
}
double zSumResultBefore = result.zSum();
double zSumClosureInDb = closureInDatabase.zSum();
result.assign(closureInDatabase, Functions.minus);
double zSumResultAfter = result.zSum();
logger.info("Summary of items in closure computed: {}, in DB-stored closure: {}, delta: {}", zSumResultBefore, zSumClosureInDb, zSumResultAfter);
if (DUMP_TC_MATRIX_DETAILS) {
logger.info("Difference matrix = {}", result);
}
boolean problem = false;
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
double delta = result.get(i, j);
if (Math.round(delta) != 0) {
System.err.println("delta(" + vertexList.get(i) + "," + vertexList.get(j) + ") = " + delta + " (closureInDB=" + closureInDatabase.get(i, j) + ", expected=" + (result.get(i, j) + closureInDatabase.get(i, j)) + ")");
logger.error("delta(" + vertexList.get(i) + "," + vertexList.get(j) + ") = " + delta);
problem = true;
}
}
}
if (problem) {
checkOrgGraph();
}
return problem;
}
}
use of org.hibernate.query.Query in project midpoint by Evolveum.
the class AbstractOrgClosureTest method _test400UnloadOrgStructure.
@UnusedTestElement
protected void _test400UnloadOrgStructure() throws Exception {
OperationResult opResult = new OperationResult("===[ unloadOrgStruct ]===");
long start = System.currentTimeMillis();
removeOrgStructure(opResult);
System.out.println("Removed in " + (System.currentTimeMillis() - start) + " ms");
try (Session session = openSession()) {
Query q = session.createNativeQuery("select count(*) from m_org_closure");
System.out.println("OrgClosure table has " + q.list().get(0) + " rows");
}
logger.info("Finish.");
}
Aggregations