Search in sources :

Example 16 with NativeQuery

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);
    }
}
Also used : NativeQuery(org.hibernate.query.NativeQuery)

Example 17 with NativeQuery

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));
    }
}
Also used : NativeQuery(org.hibernate.query.NativeQuery) Collections.singletonList(java.util.Collections.singletonList)

Example 18 with NativeQuery

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();
}
Also used : EntityManager(javax.persistence.EntityManager) NativeQuery(org.hibernate.query.NativeQuery) Test(org.junit.Test)

Example 19 with NativeQuery

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();
    }
}
Also used : NativeQuery(org.hibernate.query.NativeQuery) Session(org.hibernate.Session) Test(org.junit.Test)

Example 20 with NativeQuery

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();
}
Also used : NativeQuery(org.hibernate.query.NativeQuery)

Aggregations

NativeQuery (org.hibernate.query.NativeQuery)27 Test (org.junit.Test)8 TestForIssue (org.hibernate.testing.TestForIssue)6 SQLException (java.sql.SQLException)4 Collections.singletonList (java.util.Collections.singletonList)4 HibernateException (org.hibernate.HibernateException)4 DAOException (org.jbei.ice.storage.DAOException)4 Session (org.hibernate.Session)3 Group (org.jbei.ice.storage.model.Group)3 List (java.util.List)2 EntityManager (javax.persistence.EntityManager)2 PrismObject (com.evolveum.midpoint.prism.PrismObject)1 RObject (com.evolveum.midpoint.repo.sql.data.common.RObject)1 QueryEngine (com.evolveum.midpoint.repo.sql.query.QueryEngine)1 RQuery (com.evolveum.midpoint.repo.sql.query.RQuery)1 QueryException (com.evolveum.midpoint.repo.sqlbase.QueryException)1 ResultHandler (com.evolveum.midpoint.schema.ResultHandler)1 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 OrgType (com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType)1 ArrayList (java.util.ArrayList)1