Search in sources :

Example 21 with SQLQuery

use of org.hibernate.SQLQuery in project hibernate-orm by hibernate.

the class NativeSqlAndQuotedIdentifiersTest method testPartialScalarDiscovery.

@Test
public void testPartialScalarDiscovery() {
    Session session = openSession();
    session.beginTransaction();
    SQLQuery query = (SQLQuery) session.getNamedQuery("query-person");
    query.setResultSetMapping("person-scalar");
    query.list();
    session.getTransaction().commit();
    session.close();
}
Also used : SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session) Test(org.junit.Test)

Example 22 with SQLQuery

use of org.hibernate.SQLQuery in project JessMA by ldcsaa.

the class HibernateFacade method sqlQuery4.

/**
	 * 
	 * 根据起始记录和最大记录数执行SQL查询,并根据Entities和Joins返回查询结果
	 * 
	 * @param firstResult	: 起始记录索引
	 * @param maxResults	: 最大记录数
	 * @param sql			: SQL 查询语句
	 * @param entities		: 要绑定的查询实体集合
	 * @param joins			: 要绑定的连接实体集合
	 * @param params		: 查询参数
	 * @return				: 查询结果
	 * 
	 */
protected <T> List<T> sqlQuery4(int firstResult, int maxResults, String sql, KV<String, Object>[] entities, KV<String, String>[] joins, Object... params) {
    SQLQuery sqlQuery = getSession().createSQLQuery(sql);
    for (int i = 0; i < params.length; i++) sqlQuery.setParameter(i, params[i]);
    if (entities != null) {
        for (int i = 0; i < entities.length; i++) {
            KV<String, Object> entity = entities[i];
            String key = entity.getKey();
            Object value = entity.getValue();
            Class<?> v1 = (value instanceof Class) ? (Class<?>) value : null;
            String v2 = v1 == null ? (String) value : null;
            if (key == null || key.length() == 0) {
                if (v1 != null)
                    sqlQuery.addEntity(v1);
                else
                    sqlQuery.addEntity(v2);
            } else {
                if (v1 != null)
                    sqlQuery.addEntity(key, v1);
                else
                    sqlQuery.addEntity(key, v2);
            }
        }
    }
    if (joins != null) {
        for (KV<String, String> join : joins) sqlQuery.addJoin(join.getKey(), (String) join.getValue());
    }
    if (firstResult > 0)
        sqlQuery.setFirstResult(firstResult);
    if (maxResults > 0)
        sqlQuery.setMaxResults(maxResults);
    return sqlQuery.list();
}
Also used : SQLQuery(org.hibernate.SQLQuery)

Example 23 with SQLQuery

use of org.hibernate.SQLQuery in project midpoint by Evolveum.

the class DeleteTest method delete0003.

@Test
public void delete0003() throws Exception {
    PrismObject<ShadowType> shadow = prismContext.parseObject(new File(FOLDER_BASE, "delete/shadow.xml"));
    OperationResult result = new OperationResult("add shadow");
    final String oid = repositoryService.addObject(shadow, null, result);
    PrismObject<ShadowType> repoShadow = repositoryService.getObject(ShadowType.class, oid, null, result);
    shadow = prismContext.parseObject(new File(FOLDER_BASE, "delete/shadow.xml"));
    AssertJUnit.assertEquals(shadow, repoShadow);
    repositoryService.deleteObject(ShadowType.class, oid, result);
    result.recomputeStatus();
    AssertJUnit.assertTrue(result.isSuccess());
    Session session = getFactory().openSession();
    try {
        SQLQuery query = session.createSQLQuery("select count(*) from m_trigger where owner_oid = ?");
        query.setString(0, oid);
        Number count = (Number) query.uniqueResult();
        AssertJUnit.assertEquals(count.longValue(), 0L);
    } finally {
        session.close();
    }
}
Also used : ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) SQLQuery(org.hibernate.SQLQuery) File(java.io.File) Session(org.hibernate.Session) Test(org.testng.annotations.Test)

Example 24 with SQLQuery

use of org.hibernate.SQLQuery in project dhis2-core by dhis2.

the class HibernateGenericStore method getSqlQuery.

/**
     * Creates a SqlQuery.
     *
     * @param sql the sql query.
     * @return a SqlQuery instance.
     */
protected final SQLQuery getSqlQuery(String sql) {
    SQLQuery query = getSession().createSQLQuery(sql);
    query.setCacheable(cacheable);
    return query;
}
Also used : SQLQuery(org.hibernate.SQLQuery)

Aggregations

SQLQuery (org.hibernate.SQLQuery)24 Session (org.hibernate.Session)17 Test (org.junit.Test)13 List (java.util.List)6 ArrayList (java.util.ArrayList)3 Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Transaction (org.hibernate.Transaction)2 LimitHandler (org.hibernate.dialect.pagination.LimitHandler)2 RowSelection (org.hibernate.engine.spi.RowSelection)2 Employment (org.hibernate.test.sql.hand.Employment)2 Organization (org.hibernate.test.sql.hand.Organization)2 Person (org.hibernate.test.sql.hand.Person)2 TestForIssue (org.hibernate.testing.TestForIssue)2 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)2 ReferenceDelta (com.evolveum.midpoint.prism.delta.ReferenceDelta)1 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)1 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)1