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 338591c commit 92ebbc7
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void readDetailsFromConnection(RepositoryDiag diag, final SqlRepositoryC

EntityManagerFactory entityManagerFactory = baseHelper.getEntityManagerFactory();
// TODO THIS WILL NOT NOT WORK
xxx
//xxx
if (!(entityManagerFactory instanceof SessionFactoryImpl sessionFactoryImpl)) {
return;
}
Expand All @@ -168,7 +168,7 @@ private void readDetailsFromConnection(RepositoryDiag diag, final SqlRepositoryC
//nowhere to report error (no operation result available)
em.getTransaction().rollback();
} finally {
baseHelper.cleanupSessionAndResult(em, null);
baseHelper.cleanupManagerAndResult(em, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManager;
import org.hibernate.query.criteria.JpaCriteriaQuery;
import jakarta.persistence.TypedQuery;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -62,9 +62,8 @@ private void fetchItemsAttempt() {
try {
em = baseHelper.beginReadOnlyTransaction();

JpaCriteriaQuery<RExtItem> query = em.getCriteriaBuilder().createQuery(RExtItem.class);
query.select(query.from(RExtItem.class));
List<RExtItem> items = em.createQuery(query).getResultList();
TypedQuery<RExtItem> query = em.createQuery("from RExtItem", RExtItem.class);
List<RExtItem> items = query.getResultList();
LOGGER.debug("Fetched {} item definitions", items.size());

itemsById = new ConcurrentHashMap<>(items.size());
Expand All @@ -80,7 +79,7 @@ private void fetchItemsAttempt() {
LOGGER.debug("Exception fetch: {}", ex.getMessage());
baseHelper.handleGeneralException(ex, em, null);
} finally {
baseHelper.cleanupSessionAndResult(em, null);
baseHelper.cleanupManagerAndResult(em, null);
}
}

Expand Down Expand Up @@ -137,7 +136,7 @@ private void addExtItemAttempt(RExtItem item) {
} catch (RuntimeException ex) {
baseHelper.handleGeneralException(ex, em, null);
} finally {
baseHelper.cleanupSessionAndResult(em, null);
baseHelper.cleanupManagerAndResult(em, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void rollbackTransaction(
em.getTransaction().rollback();
}

public void cleanupSessionAndResult(EntityManager em, OperationResult result) {
public void cleanupManagerAndResult(EntityManager em, OperationResult result) {
if (em != null && em.getTransaction().isActive()) {
em.getTransaction().commit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import jakarta.persistence.Query;
import jakarta.persistence.TypedQuery;
import org.apache.commons.collections4.CollectionUtils;
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
import org.hibernate.query.criteria.JpaCriteriaQuery;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -399,11 +397,9 @@ <T extends ObjectType> void updateLoadedCampaign(

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

HibernateCriteriaBuilder cb = em.getCriteriaBuilder();
JpaCriteriaQuery<RAccessCertificationCase> cq = cb.createQuery(RAccessCertificationCase.class);
cq.where(cb.equal(cq.from(RAccessCertificationCase.class).get("ownerOid"), object.getOid()));

TypedQuery<RAccessCertificationCase> query = em.createQuery(cq);
TypedQuery<RAccessCertificationCase> query = em.createQuery(
"from RAccessCertificationCase c where c.ownerOid = :oid", RAccessCertificationCase.class)
.setParameter("oid", object.getOid());

// TODO fetch only XML representation
List<RAccessCertificationCase> cases = query.getResultList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void resolveNamesIfRequested(EntityManager em, List<? extends PrismContai
batch.add(iterator.next());
if (batch.size() >= MAX_OIDS_TO_RESOLVE_AT_ONCE || !iterator.hasNext()) {
Query query = em.createNamedQuery("resolveReferences");
query.setParameterList("oid", batch);
query.setParameter("oid", batch);

@SuppressWarnings({ "unchecked", "raw" })
List<Object[]> results = query.getResultList(); // returns oid + name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@
*/
package com.evolveum.midpoint.repo.sql.helpers;

import static com.evolveum.midpoint.schema.GetOperationOptions.isAllowNotFound;

import static org.apache.commons.lang3.ArrayUtils.getLength;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;

import static com.evolveum.midpoint.schema.GetOperationOptions.isAllowNotFound;
import static com.evolveum.midpoint.schema.result.OperationResultStatus.FATAL_ERROR;
import static com.evolveum.midpoint.schema.result.OperationResultStatus.HANDLED_ERROR;

import java.util.*;
import java.util.stream.Collectors;
import javax.xml.namespace.QName;

import jakarta.persistence.EntityManager;
import jakarta.persistence.LockModeType;
import jakarta.persistence.Query;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import javax.xml.namespace.QName;

import org.hibernate.*;
import org.hibernate.query.NativeQuery;
import jakarta.persistence.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
Expand Down Expand Up @@ -114,7 +113,7 @@ public <T extends ObjectType> PrismObject<T> getObjectAttempt(Class<T> type, Str
} catch (DtoTranslationException | RuntimeException ex) {
baseHelper.handleGeneralException(ex, em, result);
} finally {
baseHelper.cleanupSessionAndResult(em, result);
baseHelper.cleanupManagerAndResult(em, result);
}

if (LOGGER.isTraceEnabled()) {
Expand All @@ -131,11 +130,11 @@ public <T extends ObjectType> PrismObject<T> getObjectInternal(EntityManager em,
boolean lockedForUpdateViaHibernate = false;
boolean lockedForUpdateViaSql = false;

LockOptions lockOptions = new LockOptions();
LockModeType lockMode = LockModeType.NONE;
//todo fix lock for update!!!!!
if (lockForUpdate) {
if (getConfiguration().isLockForUpdateViaHibernate()) {
lockOptions.setLockMode(LockMode.PESSIMISTIC_WRITE);
lockMode = LockModeType.PESSIMISTIC_WRITE;
lockedForUpdateViaHibernate = true;
} else if (getConfiguration().isLockForUpdateViaSql()) {
LOGGER.trace("Trying to lock object {} for update (via SQL)", oid);
Expand Down Expand Up @@ -168,7 +167,7 @@ public <T extends ObjectType> PrismObject<T> getObjectInternal(EntityManager em,
Query query = em.createNamedQuery("get.object");
query.setParameter("oid", oid);
query.setResultTransformer(GetObjectResult.RESULT_STYLE.getResultTransformer());
query.setLockOptions(lockOptions);
query.setLockMode(lockMode);

fullObject = (GetObjectResult) query.getSingleResult();
} else {
Expand All @@ -184,7 +183,7 @@ public <T extends ObjectType> PrismObject<T> getObjectInternal(EntityManager em,
cq.where(cb.equal(cq.from(clazz).get("oid"), oid));

Query query = em.createQuery(cq);
query.setLockOptions(lockOptions);
query.setLockMode(lockMode);

RObject obj = (RObject) query.getSingleResult();

Expand Down Expand Up @@ -256,7 +255,7 @@ public <T extends ObjectType> int countObjectsAttempt(Class<T> type, ObjectQuery
} catch (QueryException | RuntimeException ex) {
baseHelper.handleGeneralException(ex, em, result);
} finally {
baseHelper.cleanupSessionAndResult(em, result);
baseHelper.cleanupManagerAndResult(em, result);
}

return count;
Expand Down Expand Up @@ -305,7 +304,7 @@ public <C extends Containerable> int countContainersAttempt(Class<C> type, Objec
baseHelper.handleGeneralException(ex, em, result);
throw new AssertionError("Shouldn't get here; previous method call should throw an exception.");
} finally {
baseHelper.cleanupSessionAndResult(em, result);
baseHelper.cleanupManagerAndResult(em, result);
}
}

Expand Down Expand Up @@ -333,7 +332,7 @@ public <T extends ObjectType> SearchResultList<PrismObject<T>> searchObjectsAtte
baseHelper.handleGeneralException(ex, em, result);
throw new IllegalStateException("shouldn't get here");
} finally {
baseHelper.cleanupSessionAndResult(em, result);
baseHelper.cleanupManagerAndResult(em, result);
}
}

Expand Down Expand Up @@ -436,7 +435,7 @@ private <T extends ObjectType> List<PrismObject<T>> queryResultToPrismObjects(
} catch (QueryException | DtoTranslationException | RuntimeException ex) {
baseHelper.handleGeneralException(ex, em, result);
} finally {
baseHelper.cleanupSessionAndResult(em, result);
baseHelper.cleanupManagerAndResult(em, result);
}

list.forEach(c -> ObjectTypeUtil.normalizeAllRelations(c.asPrismContainerValue(), relationRegistry));
Expand Down Expand Up @@ -779,7 +778,7 @@ public <T extends ObjectType> String getVersionAttempt(Class<T> type, String oid
} catch (RuntimeException ex) {
baseHelper.handleGeneralRuntimeException(ex, em, result);
} finally {
baseHelper.cleanupSessionAndResult(em, result);
baseHelper.cleanupManagerAndResult(em, result);
}

return version;
Expand Down Expand Up @@ -828,7 +827,7 @@ public <T extends ObjectType> void searchObjectsIterativeAttempt(Class<T> type,
} catch (SchemaException | QueryException | RuntimeException | ObjectNotFoundException ex) {
baseHelper.handleGeneralException(ex, em, result);
} finally {
baseHelper.cleanupSessionAndResult(em, result);
baseHelper.cleanupManagerAndResult(em, result);
retrievedOids.addAll(newlyRetrievedOids);
}
}
Expand Down Expand Up @@ -983,7 +982,7 @@ public boolean isAnySubordinateAttempt(String upperOrgOid, Collection<String> lo
query.setParameter("dOid", lowerObjectOids.iterator().next());
} else {
query = session.createNamedQuery("isAnySubordinateAttempt.moreLowerOids");
query.setParameterList("dOids", lowerObjectOids);
query.setParameter("dOids", lowerObjectOids);
}
query.setParameter("aOid", upperOrgOid);

Expand All @@ -994,7 +993,7 @@ public boolean isAnySubordinateAttempt(String upperOrgOid, Collection<String> lo
} catch (RuntimeException ex) {
baseHelper.handleGeneralException(ex, session, null);
} finally {
baseHelper.cleanupSessionAndResult(session, null);
baseHelper.cleanupManagerAndResult(session, null);
}

throw new SystemException("isAnySubordinateAttempt failed somehow, this really should not happen.");
Expand Down Expand Up @@ -1038,7 +1037,7 @@ public RepositoryQueryDiagResponse executeQueryDiagnosticsRequest(RepositoryQuer
baseHelper.handleGeneralException(ex, em, result);
throw new IllegalStateException("shouldn't get here");
} finally {
baseHelper.cleanupSessionAndResult(em, result);
baseHelper.cleanupManagerAndResult(em, result);
}
}

Expand Down

0 comments on commit 92ebbc7

Please sign in to comment.