Search in sources :

Example 11 with Query

use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.

the class EntityManagerImpl method createNativeQuery.

@Override
public Query createNativeQuery(String sql) {
    Query query = createQueryInstance(true, null);
    query.setQueryString(sql);
    return query;
}
Also used : Query(com.haulmont.cuba.core.Query) TypedQuery(com.haulmont.cuba.core.TypedQuery)

Example 12 with Query

use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.

the class EntityManagerImpl method findWithViews.

protected <T extends Entity> T findWithViews(MetaClass metaClass, Object id, List<View> views) {
    Object realId = getRealId(id);
    log.debug("find {} by id={}, views={}", metaClass.getJavaClass().getSimpleName(), realId, views);
    String pkName = metadata.getTools().getPrimaryKeyName(metaClass);
    if (pkName == null)
        throw new IllegalStateException("Cannot determine PK name for entity " + metaClass);
    Query query = createQuery(String.format("select e from %s e where e.%s = ?1", metaClass.getName(), pkName));
    ((QueryImpl) query).setSingleResultExpected(true);
    query.setParameter(1, realId);
    for (View view : views) {
        query.addView(view);
    }
    // noinspection unchecked
    return (T) query.getFirstResult();
}
Also used : Query(com.haulmont.cuba.core.Query) TypedQuery(com.haulmont.cuba.core.TypedQuery)

Example 13 with Query

use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.

the class UserSettingServiceTest method tearDown.

@After
public void tearDown() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Query q = em.createNativeQuery("delete from SEC_USER_SETTING where USER_ID = ?");
        q.setParameter(1, TestUserSessionSource.USER_ID);
        q.executeUpdate();
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query) After(org.junit.After)

Example 14 with Query

use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.

the class SoftDeleteTest method testQuery_CleanupMode.

@Test
public void testQuery_CleanupMode() {
    System.out.println("===================== BEGIN testQuery_CleanupMode =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        em.setSoftDeletion(false);
        Query query = em.createQuery("select r from sec$Role r where r.name = ?1");
        query.setParameter(1, "roleToBeDeleted");
        List<Role> list = query.getResultList();
        assertTrue(!list.isEmpty());
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testQuery_CleanupMode =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query)

Example 15 with Query

use of com.haulmont.cuba.core.Query in project cuba by cuba-platform.

the class SoftDeleteTest method testUpdateQuery_CleanupMode.

@Test
public void testUpdateQuery_CleanupMode() {
    System.out.println("===================== BEGIN testUpdateQuery_CleanupMode =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        em.setSoftDeletion(false);
        Query query = em.createQuery("update sec$Role r set r.description = ?1 where r.name = ?2");
        query.setParameter(1, "Updated");
        query.setParameter(2, "roleToBeDeleted");
        int updated = query.executeUpdate();
        assertEquals(1, updated);
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testUpdateQuery_CleanupMode =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) Query(com.haulmont.cuba.core.Query)

Aggregations

Query (com.haulmont.cuba.core.Query)42 EntityManager (com.haulmont.cuba.core.EntityManager)27 Transaction (com.haulmont.cuba.core.Transaction)25 TypedQuery (com.haulmont.cuba.core.TypedQuery)12 Entity (com.haulmont.cuba.core.entity.Entity)7 List (java.util.List)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)5 MetaClass (com.haulmont.chile.core.model.MetaClass)3 Date (java.util.Date)3 TransactionParams (com.haulmont.cuba.core.TransactionParams)2 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)2 QueryImpl (com.haulmont.cuba.core.sys.QueryImpl)2 User (com.haulmont.cuba.security.entity.User)2 After (org.junit.After)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Logger (ch.qos.logback.classic.Logger)1 LoggerContext (ch.qos.logback.classic.LoggerContext)1 StringHelper (com.haulmont.bali.util.StringHelper)1 EnumClass (com.haulmont.chile.core.datatypes.impl.EnumClass)1 PersistenceSecurity (com.haulmont.cuba.core.PersistenceSecurity)1