use of org.hibernate.query.NativeQuery in project midpoint by Evolveum.
the class OrgClosureManager method handleDelete.
// endregion
// region Handling DELETE operation
private void handleDelete(String oid, Context context, Session session) {
List<String> livingChildren = getChildren(oid, session);
if (livingChildren.isEmpty()) {
handleDeleteLeaf(oid, session);
return;
}
// delete all edges "<child> -> OID" from the closure
removeChildrenEdges(oid, livingChildren, context, session);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Deleted {} 'child' links.", livingChildren.size());
}
// delete all edges "OID -> <parent>" from the closure
List<String> livingParents = retainExistingOids(getParents(oid, session), session);
removeParentEdges(oid, livingParents, context, session);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Deleted {} 'parent' links.", livingParents.size());
}
// delete (OID, OID) record
NativeQuery deleteSelfQuery = session.createNativeQuery("delete from " + CLOSURE_TABLE_NAME + " " + "where descendant_oid=:oid and ancestor_oid=:oid");
deleteSelfQuery.setParameter("oid", oid);
int count = deleteSelfQuery.executeUpdate();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Removed {} self-record from closure table.", count);
}
}
use of org.hibernate.query.NativeQuery in project midpoint by Evolveum.
the class OrgClosureManager method checkForCycles.
// Checks that there is no edge=(D,A) such that A->D exists in the transitive closure
// (this would yield a cycle D->A->D in the graph)
private void checkForCycles(List<Edge> edges, Session session) {
String queryText = "select descendant_oid, ancestor_oid from " + CLOSURE_TABLE_NAME + " where " + getWhereClauseForCycleCheck(edges);
NativeQuery query = session.createNativeQuery(queryText).addScalar("descendant_oid", StringType.INSTANCE).addScalar("ancestor_oid", StringType.INSTANCE);
long start = System.currentTimeMillis();
List list = query.list();
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));
}
}
use of org.hibernate.query.NativeQuery in project hibernate-orm by hibernate.
the class QueryLockingTest method testNativeSql.
@Test
public void testNativeSql() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
NativeQuery query = em.createNativeQuery("select * from lockable l").unwrap(NativeQuery.class);
// the spec disallows calling setLockMode in a native SQL query
try {
query.setLockMode(LockModeType.READ);
fail("Should have failed");
} catch (IllegalStateException expected) {
}
// however, we should be able to set it using hints
query.setHint(QueryHints.HINT_NATIVE_LOCKMODE, LockModeType.READ);
// NOTE : LockModeType.READ should map to LockMode.OPTIMISTIC
assertEquals(LockMode.OPTIMISTIC, query.getLockOptions().getLockMode());
assertNull(query.getLockOptions().getAliasSpecificLockMode("l"));
assertEquals(LockMode.OPTIMISTIC, query.getLockOptions().getEffectiveLockMode("l"));
query.setHint(AvailableSettings.ALIAS_SPECIFIC_LOCK_MODE + ".l", LockModeType.PESSIMISTIC_WRITE);
assertEquals(LockMode.OPTIMISTIC, query.getLockOptions().getLockMode());
assertEquals(LockMode.PESSIMISTIC_WRITE, query.getLockOptions().getAliasSpecificLockMode("l"));
assertEquals(LockMode.PESSIMISTIC_WRITE, query.getLockOptions().getEffectiveLockMode("l"));
em.getTransaction().commit();
em.close();
}
use of org.hibernate.query.NativeQuery in project hibernate-orm by hibernate.
the class NativeQueryDoesNotSupportIterationTest method iterateShouldThrowAnUnsupportedOperationException.
@Test(expected = UnsupportedOperationException.class)
public void iterateShouldThrowAnUnsupportedOperationException() {
try (Session session = openSession()) {
final NativeQuery sqlQuery = session.createNativeQuery("select * from TEST_ENTITY");
sqlQuery.iterate();
}
}
use of org.hibernate.query.NativeQuery in project CyberpunkZombieSurvival_JVM by zunath.
the class DataContext method executeUpdateOrDelete.
public void executeUpdateOrDelete(String sqlFilePath, SqlParameter... params) {
String sql = readSQL(sqlFilePath);
NativeQuery query = getSession().createNativeQuery(sql);
for (SqlParameter p : params) {
query.setParameter(p.getName(), p.getValue());
}
query.executeUpdate();
}
Aggregations