Skip to content

Commit

Permalink
switch from session/sessionFactory to entityManager. still not compil…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
1azyman committed Apr 26, 2024
1 parent 92ebbc7 commit be691f7
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(
// hibernateProperties.setProperty("hibernate.implicit_naming_strategy ", MidPointImplicitNamingStrategy.class.getName());
// hibernateProperties.setProperty("hibernate.physical_naming_strategy", MidPointPhysicalNamingStrategy.class.getName());

bean.setHibernateProperties(hibernateProperties);
bean.setJpaProperties(hibernateProperties);
bean.setImplicitNamingStrategy(midPointImplicitNamingStrategy);
bean.setPhysicalNamingStrategy(midPointPhysicalNamingStrategy);
bean.setAnnotatedPackages("com.evolveum.midpoint.repo.sql.type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
package com.evolveum.midpoint.repo.sql.data.common;

import java.io.Serializable;
import jakarta.persistence.*;

import jakarta.persistence.*;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.ForeignKey;

Expand All @@ -18,6 +18,24 @@
import com.evolveum.midpoint.repo.sql.query.definition.NotQueryable;
import com.evolveum.midpoint.repo.sql.util.RUtil;

@SqlResultSetMapping(name = "OrgClosureBasic",
columns = {
@ColumnResult(name = "descendant_oid", type = String.class),
@ColumnResult(name = "ancestor_oid", type = String.class),
@ColumnResult(name = "val", type = Integer.class),
}
)
@SqlResultSetMapping(name = "OrgClosureQuickCheck",
columns = {
@ColumnResult(name = "problems", type = Integer.class)
}
)
@SqlResultSetMapping(name = "OrgClosureCheckCycles",
columns = {
@ColumnResult(name = "descendant_oid", type = String.class),
@ColumnResult(name = "ancestor_oid", type = String.class),
}
)
@Ignore
@IdClass(ROrgClosureId.class)
@Entity
Expand Down Expand Up @@ -129,13 +147,13 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
if (this == obj) { return true; }
if (obj == null || getClass() != obj.getClass()) { return false; }
if (this == obj) {return true;}
if (obj == null || getClass() != obj.getClass()) {return false;}

ROrgClosure that = (ROrgClosure) obj;

if (ancestor != null ? !ancestor.equals(that.ancestor) : that.ancestor != null) { return false; }
if (descendant != null ? !descendant.equals(that.descendant) : that.descendant != null) { return false; }
if (ancestor != null ? !ancestor.equals(that.ancestor) : that.ancestor != null) {return false;}
if (descendant != null ? !descendant.equals(that.descendant) : that.descendant != null) {return false;}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,11 @@ public <T extends ObjectType> PrismObject<T> getObjectInternal(EntityManager em,
if (!lockForUpdate) {
Query query = em.createNamedQuery("get.object");
query.setParameter("oid", oid);
query.setResultTransformer(GetObjectResult.RESULT_STYLE.getResultTransformer());
query.setLockMode(lockMode);

query.unwrap(org.hibernate.query.Query.class)
.setResultTransformer(GetObjectResult.RESULT_STYLE.getResultTransformer());

fullObject = (GetObjectResult) query.getSingleResult();
} else {
// we're doing update after this get, therefore we load full object right now
Expand Down Expand Up @@ -1017,7 +1019,8 @@ public RepositoryQueryDiagResponse executeQueryDiagnosticsRequest(RepositoryQuer
QueryEngine engine = new QueryEngine(getConfiguration(), extItemDictionary, prismContext, relationRegistry);
RQueryImpl rQuery = (RQueryImpl) engine.interpret(request.getQuery(), type, request.getOptions(), false, em);
query = rQuery.getQuery();
implementationLevelQuery = query.getQueryString();

implementationLevelQuery = query.unwrap(org.hibernate.query.Query.class).getQueryString();
implementationLevelQueryParameters = new HashMap<>();
for (Map.Entry<String, QueryParameterValue> entry : rQuery.getQuerySource().getParameters().entrySet()) {
implementationLevelQueryParameters.put(entry.getKey(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import jakarta.persistence.Query;
import org.apache.commons.lang3.mutable.MutableInt;
import org.hibernate.Session;
import org.hibernate.query.NativeQuery;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.DependsOn;
Expand Down Expand Up @@ -337,10 +335,8 @@ private void rebuild(boolean check, boolean rebuild, boolean stopOnFailure, fina
List existingEntries = null;
if (check) {
LOGGER.info("Reading from existing org closure table");
Query selectQuery = em.createNativeQuery("SELECT descendant_oid, ancestor_oid, val from " + CLOSURE_TABLE_NAME)
.addScalar("descendant_oid", StandardBasicTypes.STRING)
.addScalar("ancestor_oid", StandardBasicTypes.STRING)
.addScalar("val", StandardBasicTypes.INTEGER);
Query selectQuery = em.createNativeQuery(
"SELECT descendant_oid, ancestor_oid, val from " + CLOSURE_TABLE_NAME, "OrgClosureBasic");
existingEntries = selectQuery.getResultList();
LOGGER.info("{} entries read", existingEntries.size());
}
Expand Down Expand Up @@ -371,10 +367,8 @@ private void rebuild(boolean check, boolean rebuild, boolean stopOnFailure, fina

if (check) {
LOGGER.info("Reading from recomputed org closure table");
Query selectQuery = em.createNativeQuery("SELECT descendant_oid, ancestor_oid, val from " + CLOSURE_TABLE_NAME)
.addScalar("descendant_oid", StandardBasicTypes.STRING)
.addScalar("ancestor_oid", StandardBasicTypes.STRING)
.addScalar("val", StandardBasicTypes.INTEGER);
Query selectQuery = em.createNativeQuery(
"SELECT descendant_oid, ancestor_oid, val from " + CLOSURE_TABLE_NAME, "OrgClosureBasic");
List recomputedEntries = selectQuery.getResultList();
LOGGER.info("{} entries read", recomputedEntries.size());
compareOrgClosureTables(existingEntries, recomputedEntries, rebuild, result);
Expand Down Expand Up @@ -418,7 +412,7 @@ private int quickCheck(EntityManager em) {
Query q = em.createNativeQuery(
"select count(m_org.oid) as problems from m_org left join m_org_closure cl " +
"on cl.descendant_oid = m_org.oid and cl.ancestor_oid = m_org.oid " +
"where cl.descendant_oid is null").addScalar("problems", StandardBasicTypes.INTEGER);
"where cl.descendant_oid is null", "OrgClosureQuickCheck");
List problemsList = q.getResultList();
if (problemsList == null || problemsList.size() != 1) {
throw new IllegalStateException("Unexpected return value from the closure check query: " + problemsList + " (a 1-item list of Integer expected)");
Expand Down Expand Up @@ -644,11 +638,9 @@ private void addIndependentEdgesInternal(List<Edge> edges, Context context, Enti
// (this would yield a cycle D->A->D in the graph)
private void checkForCycles(List<Edge> edges, EntityManager em) {
String queryText = "select descendant_oid, ancestor_oid from " + CLOSURE_TABLE_NAME + " where " + getWhereClauseForCycleCheck(edges);
NativeQuery query = em.createNativeQuery(queryText)
.addScalar("descendant_oid", StandardBasicTypes.STRING)
.addScalar("ancestor_oid", StandardBasicTypes.STRING);
Query query = em.createNativeQuery(queryText, "OrgClosureCheckCycles");
long start = System.currentTimeMillis();
List list = query.list();
List list = query.getResultList();
LOGGER.trace("Cycles checked in {} ms, {} conflicts found", System.currentTimeMillis() - start, list.size());
if (!list.isEmpty()) {
throw new IllegalArgumentException("Modification couldn't be executed, because a cycle in org structure graph would be created. Cycle-creating edges being added: " + formatList(list));
Expand Down Expand Up @@ -1029,12 +1021,9 @@ private String getWhereClause(List<Edge> edges) {
}

private void dumpOrgClosureTypeTable(EntityManager em, String tableName) {
NativeQuery<Object[]> q = em.createNativeQuery(
"select descendant_oid, ancestor_oid, val from " + tableName, Object[].class)
.addScalar("descendant_oid", StandardBasicTypes.STRING)
.addScalar("ancestor_oid", StandardBasicTypes.STRING)
.addScalar("val", StandardBasicTypes.INTEGER);
List<Object[]> list = q.list();
Query q = em.createNativeQuery(
"select descendant_oid, ancestor_oid, val from " + tableName, "OrgClosureBasic");
List<Object[]> list = q.getResultList();
LOGGER.trace("{} ({} rows):", tableName, list.size());
for (Object[] row : list) {
LOGGER.trace(" - [d={}, a={}, val={}]", row);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Collection;

import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;

import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.PrismContext;
Expand All @@ -25,8 +26,6 @@
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentHolderType;

import jakarta.persistence.Query;

/**
* @author lazyman
*/
Expand Down Expand Up @@ -58,10 +57,11 @@ public RQuery interpret(ObjectQuery query, Class<? extends Containerable> type,
Query hqlQuery = hibernateQuery.getAsHqlQuery(em);

if (LOGGER.isTraceEnabled()) {
String str = hqlQuery.unwrap(org.hibernate.query.Query.class).getQueryString();
LOGGER.trace("Query interpretation result:\n--- Query:\n{}\n--- with options: {}\n--- resulted in HQL:\n{}",
DebugUtil.debugDump(query), options, hqlQuery.getQueryString());

DebugUtil.debugDump(query), options, str);
}

return new RQueryImpl(hqlQuery, hibernateQuery);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public <T> T uniqueResult() throws HibernateException {
}

@Override
public ScrollableResults scroll(ScrollMode mode) throws HibernateException {
return query.scroll(mode);
public ScrollableResults<?> scroll(ScrollMode mode) throws HibernateException {
return query.unwrap(org.hibernate.query.Query.class).scroll(mode);
}

public Query getQuery() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.query.BindableType;
import org.hibernate.transform.ResultTransformer;
import org.hibernate.type.Type;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -141,19 +140,7 @@ public Query getAsHqlQuery(EntityManager em) {
QueryParameterValue parameterValue = parameter.getValue();
LOGGER.trace("Parameter {} = {}", name, parameterValue.debugDump());

if (parameterValue.getValue() instanceof Collection) {
if (parameterValue.getType() instanceof BindableType) {
query.setParameter(name, (Collection) parameterValue.getValue(), (BindableType) parameterValue.getType());
} else {
query.setParameter(name, parameterValue.getValue());
}
} else {
if (parameterValue.getType() instanceof BindableType) {
query.setParameter(name, parameterValue.getValue(), (BindableType) parameterValue.getType());
} else {
query.setParameter(name, parameterValue.getValue());
}
}
query.setParameter(name, parameterValue.getValue());
}
if (maxResults != null) {
query.setMaxResults(maxResults);
Expand All @@ -163,7 +150,8 @@ public Query getAsHqlQuery(EntityManager em) {
}
if (resultTransformer != null) {
//noinspection deprecation
query.setResultTransformer(resultTransformer);
query.unwrap(org.hibernate.query.Query.class)
.setResultTransformer(resultTransformer);
}
return query;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ public class GetObjectResult implements Serializable {
ROExtReference.class, ROExtPolyString.class, ROExtBoolean.class};

public static final ResultStyle RESULT_STYLE = new ResultStyle() {

@Override
public ResultTransformer getResultTransformer() {
return new ResultTransformer() {
@Override
public Object transformTuple(Object[] tuple, String[] aliases) {
return new GetObjectResult((String) tuple[0], (byte[]) tuple[1]);
}
};
return (tuple, aliases) -> new GetObjectResult((String) tuple[0], (byte[]) tuple[1]);
}

@Override
Expand Down

0 comments on commit be691f7

Please sign in to comment.