本文整理匯總了Java中com.haulmont.cuba.core.Query類的典型用法代碼示例。如果您正苦於以下問題:Java Query類的具體用法?Java Query怎麽用?Java Query使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Query類屬於com.haulmont.cuba.core包,在下文中一共展示了Query類的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);
}
示例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;
}
}
示例3: getAvailableUsers
import com.haulmont.cuba.core.Query; //導入依賴的package包/類
@Override
public List<String> getAvailableUsers() {
List<String> result = new ArrayList<>();
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
Query query = em.createQuery("select u from sec$User u");
List<User> userList = query.getResultList();
for (User user : userList) {
result.add(user.getLogin());
}
tx.commit();
} finally {
tx.end();
}
return result;
}
示例4: 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;
}
示例5: clearScheduledTasks
import com.haulmont.cuba.core.Query; //導入依賴的package包/類
@Override
public void clearScheduledTasks() {
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
Query query = em.createNativeQuery("delete from SYS_SCHEDULED_EXECUTION");
query.executeUpdate();
query = em.createNativeQuery("delete from SYS_SCHEDULED_TASK");
query.executeUpdate();
tx.commit();
} finally {
tx.end();
}
}
示例6: checkSequenceExists
import com.haulmont.cuba.core.Query; //導入依賴的package包/類
protected void checkSequenceExists(String entityName, long startValue, long increment) {
String seqName = getSequenceName(entityName);
if (existingSequences.contains(seqName))
return;
// Create sequence in separate transaction because it's name is cached and we want to be sure it is created
// regardless of possible errors in the invoking code
Transaction tx = persistence.createTransaction(getDataStore(entityName));
try {
EntityManager em = persistence.getEntityManager(getDataStore(entityName));
SequenceSupport sequenceSupport = getSequenceSupport(entityName);
Query query = em.createNativeQuery(sequenceSupport.sequenceExistsSql(seqName));
List list = query.getResultList();
if (list.isEmpty()) {
query = em.createNativeQuery(sequenceSupport.createSequenceSql(seqName, startValue, increment));
query.executeUpdate();
}
existingSequences.add(seqName);
tx.commit();
} finally {
tx.end();
}
}
示例7: resultsAlreadySaved
import com.haulmont.cuba.core.Query; //導入依賴的package包/類
private boolean resultsAlreadySaved(Integer queryKey, LoadContext.Query query) {
LinkedHashMap<Integer, QueryHolder> recentQueries =
userSessionSource.getUserSession().getAttribute("_recentQueries");
if (recentQueries == null) {
recentQueries = new LinkedHashMap<Integer, QueryHolder>() {
private static final long serialVersionUID = -901296839279897248L;
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, QueryHolder> eldest) {
return size() > 10;
}
};
}
QueryHolder queryHolder = new QueryHolder(query);
QueryHolder oldQueryHolder = recentQueries.put(queryKey, queryHolder);
// do not set to session attribute recentQueries directly, it contains reference to QueryResultsManager class
// copy data to new LinkedHashMap
userSessionSource.getUserSession().setAttribute("_recentQueries", new LinkedHashMap<>(recentQueries));
return queryHolder.equals(oldQueryHolder);
}
示例8: 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();
}
示例9: 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();
}
示例10: 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
}
示例11: getResultListUserByLoginNamed
import com.haulmont.cuba.core.Query; //導入依賴的package包/類
protected User getResultListUserByLoginNamed(User loadedUser, boolean checkView, Consumer<EntityManager> emBuilder, Consumer<Query> queryBuilder) throws Exception {
User user;
try (Transaction tx = cont.persistence().createTransaction()) {
EntityManager em = cont.entityManager();
if (emBuilder != null) {
emBuilder.accept(em);
}
TypedQuery<User> query = em.createQuery("select u from sec$User u where u.login = :login", User.class);
query.setCacheable(true);
query.setParameter("login", "ECTest-" + loadedUser.getId());
query.setViewName("user.browse");
if (queryBuilder != null) {
queryBuilder.accept(query);
}
List<User> resultList = query.getResultList();
user = resultList.isEmpty() ? null : resultList.get(0);
tx.commit();
}
if (user != null && checkView) {
assertUserBrowseView(user);
}
return user;
}
示例12: getSingleResultUserByLoginNamed
import com.haulmont.cuba.core.Query; //導入依賴的package包/類
protected User getSingleResultUserByLoginNamed(User loadedUser, Consumer<Query> queryBuilder) throws Exception {
User user;
try (Transaction tx = cont.persistence().createTransaction()) {
TypedQuery<User> query = cont.entityManager().createQuery("select u from sec$User u where u.login = ?1", User.class);
query.setCacheable(true);
query.setParameter(1, "ECTest-" + loadedUser.getId());
query.setViewName("user.browse");
if (queryBuilder != null) {
queryBuilder.accept(query);
}
user = query.getSingleResult();
tx.commit();
}
if (user != null) {
assertUserBrowseView(user);
}
return user;
}
示例13: getSingleResultUserByLoginPositional
import com.haulmont.cuba.core.Query; //導入依賴的package包/類
protected User getSingleResultUserByLoginPositional(User loadedUser, Consumer<Query> queryBuilder) throws Exception {
User user;
try (Transaction tx = cont.persistence().createTransaction()) {
TypedQuery<User> query = cont.entityManager().createQuery("select u from sec$User u where u.login = ?1", User.class);
query.setCacheable(true);
query.setParameter(1, "ECTest-" + loadedUser.getId());
query.setViewName("user.browse");
if (queryBuilder != null) {
queryBuilder.accept(query);
}
user = query.getSingleResult();
tx.commit();
}
if (user != null) {
assertUserBrowseView(user);
}
return user;
}
示例14: 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 =====================");
}
示例15: 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 =====================");
}