當前位置: 首頁>>代碼示例>>Java>>正文


Java Query.setParameter方法代碼示例

本文整理匯總了Java中com.haulmont.cuba.core.Query.setParameter方法的典型用法代碼示例。如果您正苦於以下問題:Java Query.setParameter方法的具體用法?Java Query.setParameter怎麽用?Java Query.setParameter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.haulmont.cuba.core.Query的用法示例。


在下文中一共展示了Query.setParameter方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: loadUser

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
@Nullable
@Override
public User loadUser(String login) throws LoginException {
    // first check if login is a valid user's email, and find the corresponding login name
    // to pass to the super impl
    if (login == null)
        throw new IllegalArgumentException("Login is null");

    EntityManager em = persistence.getEntityManager();
    String queryStr = "select u.loginLowerCase from sec$User u where lower(u.email) = ?1 and (u.active = true or u.active is null)";

    Query q = em.createQuery(queryStr);
    q.setParameter(1, login.toLowerCase());
    List results = q.getResultList();
    if (!results.isEmpty())
        return super.loadUser((String) results.get(0));

    return super.loadUser(login);
}
 
開發者ID:pfurini,項目名稱:cuba-component-forgot-password,代碼行數:20,代碼來源:NexbitLoginWorkerBean.java

示例2: loadUser

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
@Nullable
protected User loadUser(String login) throws LoginException {
    if (login == null) {
        throw new IllegalArgumentException("Login is null");
    }

    EntityManager em = persistence.getEntityManager();
    String queryStr = "select u from sec$User u where u.loginLowerCase = ?1 and (u.active = true or u.active is null)";

    Query q = em.createQuery(queryStr);
    q.setParameter(1, login.toLowerCase());

    List list = q.getResultList();
    if (list.isEmpty()) {
        log.debug("Unable to find user: {}", login);
        return null;
    } else {
        //noinspection UnnecessaryLocalVariable
        User user = (User) list.get(0);
        return user;
    }
}
 
開發者ID:cuba-platform,項目名稱:cuba,代碼行數:23,代碼來源:AbstractAuthenticationProvider.java

示例3: isLastExecutionFinished

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
@Override
public boolean isLastExecutionFinished(ScheduledTask task, long now) {
    EntityManager em = persistence.getEntityManager();
    Query query = em.createQuery(
            "select e.finishTime from sys$ScheduledExecution e where e.task.id = ?1 and e.startTime = ?2");
    query.setParameter(1, task.getId());
    query.setParameter(2, task.getLastStartTime());
    List list = query.getResultList();
    if (list.isEmpty() || list.get(0) == null) {
        // Execution finish was not registered by some reason, so using timeout value or just return false
        boolean result = task.getTimeout() != null && task.getLastStart() + task.getTimeout() <= now;
        if (result)
            log.trace(task + ": considered finished because of timeout");
        else
            log.trace(task + ": not finished and not timed out");
        return result;
    }
    Date date = (Date) list.get(0);
    log.trace(task + ": finished at " + date.getTime());
    return true;
}
 
開發者ID:cuba-platform,項目名稱:cuba,代碼行數:22,代碼來源:DbBasedCoordinator.java

示例4: testOneToMany_JoinFetchQuery

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
@Test
public void testOneToMany_JoinFetchQuery() {
    System.out.println("===================== BEGIN testOneToMany_JoinFetchQuery =====================");

    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();

        Query q = em.createQuery("select u from sec$User u join fetch u.userRoles where u.id = ?1");
        q.setParameter(1, userId);
        User user = (User) q.getSingleResult();

        List<UserRole> userRoles = user.getUserRoles();
        assertEquals(1, userRoles.size());
        for (UserRole ur : userRoles) {
            assertNotNull(ur.getRole());
        }

        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToMany_JoinFetchQuery =====================");
}
 
開發者ID:cuba-platform,項目名稱:cuba,代碼行數:25,代碼來源:SoftDeleteTest.java

示例5: isCollectionEmpty

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
protected boolean isCollectionEmpty(MetaProperty property) {
    MetaProperty inverseProperty = property.getInverse();
    if (inverseProperty == null) {
        log.warn("Inverse property not found for property " + property);
        Collection<Entity> value = entity.getValue(property.getName());
        return value == null || value.isEmpty();
    }

    String invPropName = inverseProperty.getName();
    String collectionPkName = metadata.getTools().getPrimaryKeyName(property.getRange().asClass());

    String qlStr = "select e." + collectionPkName + " from " + property.getRange().asClass().getName() +
            " e where e." + invPropName + "." + primaryKeyName + " = ?1";

    Query query = entityManager.createQuery(qlStr);
    query.setParameter(1, entity.getId());
    query.setMaxResults(1);
    List<Entity> list = query.getResultList();

    return list.isEmpty();
}
 
開發者ID:cuba-platform,項目名稱:cuba,代碼行數:22,代碼來源:DeletePolicyProcessor.java

示例6: testQuery_CleanupMode

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
@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 =====================");
}
 
開發者ID:cuba-platform,項目名稱:cuba,代碼行數:21,代碼來源:SoftDeleteTest.java

示例7: findWithViews

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
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();
}
 
開發者ID:cuba-platform,項目名稱:cuba,代碼行數:18,代碼來源:EntityManagerImpl.java

示例8: testUpdateQuery

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
@Test
public void testUpdateQuery() throws Exception {
    appender.clearMessages();

    loadUser();

    assertEquals(2, appender.filterMessages(m -> m.contains("> SELECT")).count()); // User, Group
    appender.clearMessages();

    try (Transaction tx = cont.persistence().createTransaction()) {
        Query query = cont.entityManager().createQuery("update sec$User u set u.position = ?1 where u.loginLowerCase = ?2");
        query.setParameter(1, "new position");
        query.setParameter(2, this.user.getLoginLowerCase());
        query.executeUpdate();
        tx.commit();
    }
    appender.clearMessages();

    User u = loadUser();
    assertEquals("new position", u.getPosition());

    assertEquals(1, appender.filterMessages(m -> m.contains("> SELECT")).count()); // Group
}
 
開發者ID:cuba-platform,項目名稱:cuba,代碼行數:24,代碼來源:EntityCacheTestClass.java

示例9: testUpdateQuery_CleanupMode

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
@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 =====================");
}
 
開發者ID:cuba-platform,項目名稱:cuba,代碼行數:21,代碼來源:SoftDeleteTest.java

示例10: testOneToMany_Query

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
@Test
public void testOneToMany_Query() {
    System.out.println("===================== BEGIN testOneToMany_Query =====================");

    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();

        Query q = em.createQuery("select u from sec$User u where u.id = ?1");
        q.setParameter(1, userId);
        User user = (User) q.getSingleResult();

        List<UserRole> userRoles = user.getUserRoles();
        assertEquals(1, userRoles.size());
        for (UserRole ur : userRoles) {
            assertNotNull(ur.getRole());
        }

        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToMany_Query =====================");
}
 
開發者ID:cuba-platform,項目名稱:cuba,代碼行數:25,代碼來源:SoftDeleteTest.java

示例11: checkConditions

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
private void checkConditions(Order order, EntityManager entityManager) {
    Query query = entityManager.createQuery(
            "select count(e) from sample$Order e where e.date = ?1 and e.customer.id = ?2 and e.id <> ?3");
    query.setParameter(1, order.getDate());
    query.setParameter(2, order.getCustomer());
    query.setParameter(3, order.getId());
    Long count = (Long) query.getSingleResult();
    if (count >= 2)
        throw new TooManyOrdersException(order.getCustomer().getName(), order.getDate());
}
 
開發者ID:cuba-platform,項目名稱:sample-data-manipulation,代碼行數:11,代碼來源:OrderEntityListener.java

示例12: deleteAll

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
@Override
public void deleteAll() {
    try (Transaction tx = persistence.createTransaction()) {
        Query query = persistence.getEntityManager().createQuery(
                "delete from sample$Customer c where c.createdBy = :createdBy");
        query.setParameter("createdBy", userSessionSource.getUserSession().getUser().getLogin());
        query.executeUpdate();
        tx.commit();
    }
}
 
開發者ID:cuba-platform,項目名稱:sample-data-manipulation,代碼行數:11,代碼來源:CustomerServiceBean.java

示例13: getDiscount

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
@Override
public Double getDiscount(CustomerGrade customerGrade, ProductType productType) {
    Double value;
    try (Transaction tx = persistence.createTransaction()) {
        Query query = persistence.getEntityManager().createQuery(
                "select d.discount from sample$Discount d where d.customerGrade = :grade and d.productType = :type");
        query.setParameter("grade", customerGrade);
        query.setParameter("type", productType);
        value = (Double) query.getFirstResult();
        tx.commit();
    }
    if (value == null)
        value = 0.0;
    return value;
}
 
開發者ID:cuba-platform,項目名稱:sample-data-manipulation,代碼行數:16,代碼來源:CustomerServiceBean.java

示例14: getCollection

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
protected Collection<Entity> getCollection(MetaProperty property) {
    MetaProperty inverseProperty = property.getInverse();
    if (inverseProperty == null) {
        log.warn("Inverse property not found for property " + property);
        Collection<Entity> value = entity.getValue(property.getName());
        return value == null ? Collections.EMPTY_LIST : value;
    }

    String invPropName = inverseProperty.getName();
    String qlStr = "select e from " + property.getRange().asClass().getName() + " e where e." + invPropName + "." +
            primaryKeyName + " = ?1";

    Query query = entityManager.createQuery(qlStr);
    query.setParameter(1, entity.getId());
    List<Entity> list = query.getResultList();

    // If the property is not loaded, it means it was not modified and further check is not needed
    if (!PersistenceHelper.isLoaded(entity, property.getName())) {
        return list;
    }
    // Check whether the collection items still belong to the master entity, because they could be changed in the
    // current transaction that did not affect the database yet
    List<Entity> result = new ArrayList<>(list.size());
    for (Entity item : list) {
        Entity master = item.getValue(invPropName);
        if (entity.equals(master))
            result.add(item);
    }
    return result;
}
 
開發者ID:cuba-platform,項目名稱:cuba,代碼行數:31,代碼來源:DeletePolicyProcessor.java

示例15: cascade

import com.haulmont.cuba.core.Query; //導入方法依賴的package包/類
protected void cascade(String entityName, MetaProperty property) {
    String template = property.getRange().getCardinality().isMany() ?
            "select e from %s e join e.%s c where c." + primaryKeyName + " = ?1" :
            "select e from %s e where e.%s." + primaryKeyName + " = ?1";
    String qstr = String.format(template, entityName, property.getName());
    Query query = entityManager.createQuery(qstr);
    query.setParameter(1, entity.getId());
    List<Entity> list = query.getResultList();
    for (Entity e : list) {
        entityManager.remove(e);
    }
}
 
開發者ID:cuba-platform,項目名稱:cuba,代碼行數:13,代碼來源:DeletePolicyProcessor.java


注:本文中的com.haulmont.cuba.core.Query.setParameter方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。