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;
}
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();
}
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();
}
}
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 =====================");
}
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 =====================");
}
Aggregations