Search in sources :

Example 91 with Query

use of org.hibernate.query.Query in project OpenClinica by OpenClinica.

the class ItemDataDao method getMaxGroupRepeat.

public int getMaxGroupRepeat(Integer eventCrfId, Integer itemId) {
    Query q = getCurrentSession().createQuery(getMaxGroupRepeatQuery);
    q.setParameter("eventCrfId", eventCrfId);
    q.setParameter("itemId", itemId);
    Number result = (Number) q.uniqueResult();
    if (result == null)
        return 0;
    else
        return result.intValue();
}
Also used : Query(org.hibernate.query.Query)

Example 92 with Query

use of org.hibernate.query.Query in project api-core by ca-cwds.

the class SystemCodeDaoTest method findAll_Args__.

@Test
public void findAll_Args__() throws Exception {
    SessionFactory sessionFactory = mock(SessionFactory.class);
    Session session = mock(Session.class);
    Transaction tx = mock(Transaction.class);
    Query query = mock(Query.class);
    when(sessionFactory.getCurrentSession()).thenReturn(session);
    when(session.getNamedQuery(any(String.class))).thenReturn(query);
    when(session.beginTransaction()).thenReturn(tx);
    when(query.list()).thenReturn(new ArrayList<SystemCode>());
    when(query.setString(any(String.class), any(String.class))).thenReturn(query);
    SystemCodeDao target = new SystemCodeDao(sessionFactory);
    SystemCode[] actual = target.findByForeignKeyMetaTable("asdf");
    SystemCode[] expected = new SystemCode[0];
    assertThat(actual, is(equalTo(expected)));
}
Also used : SessionFactory(org.hibernate.SessionFactory) Transaction(org.hibernate.Transaction) Query(org.hibernate.query.Query) SystemCode(gov.ca.cwds.data.persistence.cms.SystemCode) Session(org.hibernate.Session) Test(org.junit.Test)

Example 93 with Query

use of org.hibernate.query.Query in project api-core by ca-cwds.

the class SystemMetaDaoTest method findAll_Args__.

@Test
public void findAll_Args__() throws Exception {
    SessionFactory sessionFactory = mock(SessionFactory.class);
    Session session = mock(Session.class);
    Transaction tx = mock(Transaction.class);
    Query query = mock(Query.class);
    when(sessionFactory.getCurrentSession()).thenReturn(session);
    when(session.getNamedQuery(any(String.class))).thenReturn(query);
    when(session.beginTransaction()).thenReturn(tx);
    SystemMetaDao target = new SystemMetaDao(sessionFactory);
    SystemMeta[] actual = target.findAll();
    SystemMeta[] expected = new SystemMeta[0];
    assertThat(actual, is(equalTo(expected)));
}
Also used : SessionFactory(org.hibernate.SessionFactory) Transaction(org.hibernate.Transaction) Query(org.hibernate.query.Query) SystemMeta(gov.ca.cwds.data.persistence.cms.SystemMeta) Session(org.hibernate.Session) Test(org.junit.Test)

Example 94 with Query

use of org.hibernate.query.Query in project stdlib by petergeneric.

the class JPAQueryBuilder method selectIDs.

public <ID> List<ID> selectIDs() {
    Query query = createSelectIDs();
    List<?> results = query.getResultList();
    if (results.isEmpty())
        return Collections.emptyList();
    else if (results.get(0).getClass().isArray())
        // N.B. we explicitly do not remove duplicates because the only case of duplicates appearing should be if ordering by a collection
        return results.stream().map(r -> Array.get(r, 0)).map(id -> (ID) id).collect(Collectors.toList());
    else
        return (List<ID>) results;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Arrays(java.util.Arrays) Criteria(org.hibernate.Criteria) Array(java.lang.reflect.Array) Fetch(javax.persistence.criteria.Fetch) Session(org.hibernate.Session) Selection(javax.persistence.criteria.Selection) HashMap(java.util.HashMap) WQOrder(com.peterphi.std.guice.restclient.jaxb.webquery.WQOrder) QEntity(com.peterphi.std.guice.hibernate.webquery.impl.QEntity) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) WQFunctionType(com.peterphi.std.guice.restclient.jaxb.webquery.WQFunctionType) Logger(org.apache.log4j.Logger) Predicate(javax.persistence.criteria.Predicate) Map(java.util.Map) Query(org.hibernate.query.Query) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) JoinType(javax.persistence.criteria.JoinType) NotImplementedException(com.peterphi.std.NotImplementedException) Expression(javax.persistence.criteria.Expression) WQConstraintLine(com.peterphi.std.guice.restclient.jaxb.webquery.WQConstraintLine) Path(javax.persistence.criteria.Path) Root(javax.persistence.criteria.Root) WQPath(com.peterphi.std.guice.hibernate.webquery.impl.jpa.jpafunctions.WQPath) JPAJoin(com.peterphi.std.guice.hibernate.webquery.impl.jpa.jpafunctions.JPAJoin) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ParameterExpression(javax.persistence.criteria.ParameterExpression) WQConstraint(com.peterphi.std.guice.restclient.jaxb.webquery.WQConstraint) WebQuery(com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery) Collection(java.util.Collection) Set(java.util.Set) WQGroup(com.peterphi.std.guice.restclient.jaxb.webquery.WQGroup) Collectors(java.util.stream.Collectors) WQGroupType(com.peterphi.std.guice.restclient.jaxb.webquery.WQGroupType) List(java.util.List) Order(javax.persistence.criteria.Order) WQTypeHelper(com.peterphi.std.guice.hibernate.webquery.impl.WQTypeHelper) QRelation(com.peterphi.std.guice.hibernate.webquery.impl.QRelation) Collections(java.util.Collections) Query(org.hibernate.query.Query) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) WebQuery(com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery) ArrayList(java.util.ArrayList) List(java.util.List)

Example 95 with Query

use of org.hibernate.query.Query in project KeDou by XiaoBai518.

the class UserDaoImpl method findByUsername.

/**
 * @desc 通过电话或者邮箱查询用户
 * @author zhangtianrun
 * @createDate 2018年3月29日
 * @return User
 * @throws Exception
 */
public User findByUsername(String teloremail) throws Exception {
    SessionFactory sessionFactory = super.getSessionFactory();
    String hql = "from User where userTel=:tel or userEmail=:mail";
    Query query = sessionFactory.getCurrentSession().createQuery(hql);
    query.setParameter("tel", teloremail);
    query.setParameter("mail", teloremail);
    return (User) query.uniqueResult();
}
Also used : SessionFactory(org.hibernate.SessionFactory) User(com.kedou.entity.User) Query(org.hibernate.query.Query)

Aggregations

Query (org.hibernate.query.Query)149 Test (org.junit.Test)46 Session (org.hibernate.Session)39 List (java.util.List)24 ArrayList (java.util.ArrayList)19 TestForIssue (org.hibernate.testing.TestForIssue)19 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)17 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)17 HashMap (java.util.HashMap)15 Map (java.util.Map)14 NativeQuery (org.hibernate.query.NativeQuery)14 AbstractJPATest (org.hibernate.test.jpa.AbstractJPATest)11 SessionFactory (org.hibernate.SessionFactory)9 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)7 SQLException (java.sql.SQLException)7 Collectors (java.util.stream.Collectors)7 RQuery (com.evolveum.midpoint.repo.sql.query.RQuery)6 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)6 Predicate (javax.persistence.criteria.Predicate)6 Root (javax.persistence.criteria.Root)6