org.hibernate.query.internal.QueryImpl Java Examples

The following examples show how to use org.hibernate.query.internal.QueryImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultJpaRepository.java    From ueboot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public <S> Page<S> find(String queryString, String queryCount, NamedParams params, Pageable pageable, Class<S> transformer) {
    Assert.hasText(queryString, "Query must has text!");
    Assert.hasText(queryCount, "Query count must has text!");
    Assert.notNull(params, "QueryParams must not be null!");
    Assert.notNull(pageable, "PageRequest must not be null!");
    Assert.notNull(transformer, "TransformerClass must not be null!");

    Query query = em.createQuery(queryString);

    setQueryParams(query, params);
    query.setMaxResults(pageable.getPageSize());
    query.setFirstResult((int) pageable.getOffset());

    List<S> resultList = query.unwrap(QueryImpl.class).setResultTransformer(Transformers.aliasToBean(transformer)).list();

    Query countQuery = em.createQuery(queryCount);
    setQueryParams(countQuery, params);
    Long total = (Long) countQuery.getSingleResult();

    Page<S> page = new PageImpl(resultList, pageable, total);

    return page;
}
 
Example #2
Source File: StandardItemFilterProcessorTest.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Tests basic attribute query.
 * 
 * @throws Exception
 *             - if something is wrong this exception is thrown.
 */
@Test
public void testBasicAttributeQuery() throws Exception {
    NoteItemFilter filter = new NoteItemFilter();
    AttributeFilter missingFilter = new AttributeFilter();
    missingFilter.setQname(new HibQName("ns", "name"));
    filter.getAttributeFilters().add(missingFilter);
    QueryImpl<Item> query = queryBuilder.buildQuery(filter);
    Assert.assertEquals(
            "select i from HibNoteItem i where exists "
                    + "(select a.id from HibAttribute a where a.item=i and a.qname=:param0)",
            query.getQueryString());
    missingFilter.setMissing(true);
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals(
            "select i from HibNoteItem i where not exists"
                    + " (select a.id from HibAttribute a where a.item=i and a.qname=:param0)",
            query.getQueryString());
}
 
Example #3
Source File: StandardItemFilterProcessorTest.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Tests basic stamp query.
 * 
 * @throws Exception
 *             - if something is wrong this exception is thrown.
 */
@Test
public void testBasicStampQuery() throws Exception {
    NoteItemFilter filter = new NoteItemFilter();
    StampFilter missingFilter = new StampFilter();
    missingFilter.setStampClass(EventStamp.class);
    filter.getStampFilters().add(missingFilter);
    QueryImpl<Item> query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibNoteItem i where exists (select s.id from HibStamp s "
            + "where s.item=i and s.class=HibEventStamp)", query.getQueryString());
    missingFilter.setMissing(true);
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals(
            "select i from HibNoteItem i where not exists "
                    + "(select s.id from HibStamp s where s.item=i and s.class=HibEventStamp)",
            query.getQueryString());
}
 
Example #4
Source File: StandardItemFilterProcessorTest.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Tests event stamp time range query.
 * 
 * @throws Exception
 *             - if something is wrong this exception is thrown.
 */
@Test
public void testEventStampTimeRangeQuery() throws Exception {
    NoteItemFilter filter = new NoteItemFilter();
    EventStampFilter eventFilter = new EventStampFilter();
    Period period = new Period(new DateTime("20070101T100000Z"), new DateTime("20070201T100000Z"));
    eventFilter.setPeriod(period);
    eventFilter.setTimezone(registry.getTimeZone("America/Chicago"));

    CollectionItem parent = new HibCollectionItem();
    filter.setParent(parent);
    filter.getStampFilters().add(eventFilter);
    QueryImpl<Item> query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibNoteItem i join i.parentDetails pd, "
            + "HibBaseEventStamp es where pd.primaryKey.collection=:parent and es.item=i "
            + "and ( (es.timeRangeIndex.isFloating=true and "
            + "es.timeRangeIndex.startDate < '20070201T040000' and "
            + "es.timeRangeIndex.endDate > '20070101T040000') or " + "(es.timeRangeIndex.isFloating=false and "
            + "es.timeRangeIndex.startDate < '20070201T100000Z' and "
            + "es.timeRangeIndex.endDate > '20070101T100000Z') or "
            + "(es.timeRangeIndex.startDate=es.timeRangeIndex.endDate and "
            + "(es.timeRangeIndex.startDate='20070101T040000' or "
            + "es.timeRangeIndex.startDate='20070101T100000Z')))", query.getQueryString());
}
 
Example #5
Source File: StandardItemFilterProcessorTest.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Tests event stamp query.
 * 
 * @throws Exception
 *             - if something is wrong this exception is thrown.
 */
@Test
public void testEventStampQuery() throws Exception {
    NoteItemFilter filter = new NoteItemFilter();
    EventStampFilter eventFilter = new EventStampFilter();
    CollectionItem parent = new HibCollectionItem();
    filter.setParent(parent);
    filter.setDisplayName(Restrictions.eq("test"));
    filter.setIcalUid(Restrictions.eq("icaluid"));
    // filter.setBody("body");
    filter.getStampFilters().add(eventFilter);
    QueryImpl<Item> query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibNoteItem i join i.parentDetails pd, "
            + "HibBaseEventStamp es where pd.primaryKey.collection=:parent and "
            + "i.displayName=:param1 and es.item=i and i.icalUid=:param2", query.getQueryString());

    eventFilter.setIsRecurring(true);
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibNoteItem i join i.parentDetails pd, HibBaseEventStamp "
            + "es where pd.primaryKey.collection=:parent and i.displayName=:param1 and"
            + " es.item=i and (es.timeRangeIndex.isRecurring=true or i.modifies is not null) "
            + "and i.icalUid=:param2", query.getQueryString());
}
 
Example #6
Source File: StandardItemFilterProcessorTest.java    From cosmo with Apache License 2.0 6 votes vote down vote up
/**
 * Tests content item query.
 * 
 * @throws Exception
 *             - if something is wrong this exception is thrown.
 */
@Test
public void testContentItemQuery() throws Exception {
    ContentItemFilter filter = new ContentItemFilter();
    CollectionItem parent = new HibCollectionItem();
    filter.setParent(parent);
    filter.setTriageStatusCode(Restrictions.eq(TriageStatus.CODE_DONE));
    QueryImpl<Item> query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibContentItem i join i.parentDetails pd where "
            + "pd.primaryKey.collection=:parent and i.triageStatus.code=:param1", query.getQueryString());

    filter.setTriageStatusCode(Restrictions.isNull());
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibContentItem i join i.parentDetails pd where "
            + "pd.primaryKey.collection=:parent and i.triageStatus.code is null", query.getQueryString());

    filter.setTriageStatusCode(Restrictions.eq(TriageStatus.CODE_DONE));
    filter.addOrderBy(ContentItemFilter.ORDER_BY_TRIAGE_STATUS_RANK_ASC);
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibContentItem i join i.parentDetails pd where "
            + "pd.primaryKey.collection=:parent and i.triageStatus.code=:param1 order by " + "i.triageStatus.rank",
            query.getQueryString());
}
 
Example #7
Source File: AbstractSharedSessionContract.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public QueryImplementor createQuery(String queryString) {
	checkOpen();
	checkTransactionSynchStatus();
	delayedAfterCompletion();

	try {
		final QueryImpl query = new QueryImpl(
				this,
				getQueryPlan( queryString, false ).getParameterMetadata(),
				queryString
		);
		query.setComment( queryString );
		applyQuerySettingsAndHints( query );
		return query;
	}
	catch (RuntimeException e) {
		markForRollbackOnly();
		throw exceptionConverter.convert( e );
	}
}
 
Example #8
Source File: AbstractSharedSessionContract.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected QueryImplementor createQuery(NamedQueryDefinition queryDefinition) {
		String queryString = queryDefinition.getQueryString();
		final QueryImpl query = new QueryImpl(
				this,
				getQueryPlan( queryString, false ).getParameterMetadata(),
				queryString
		);
		query.setHibernateFlushMode( queryDefinition.getFlushMode() );
		query.setComment( queryDefinition.getComment() != null ? queryDefinition.getComment() : queryDefinition.getName() );
		if ( queryDefinition.getLockOptions() != null ) {
			query.setLockOptions( queryDefinition.getLockOptions() );
		}

		initQueryFromNamedDefinition( query, queryDefinition );
//		applyQuerySettingsAndHints( query );

		return query;
	}
 
Example #9
Source File: DefaultJpaRepository.java    From ueboot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public <S> Page<S> find(String queryString, String queryCount, NamedParams params, Pageable pageable, ResultTransformer transformer) {
    Assert.hasText(queryString, "Query must has text!");
    Assert.hasText(queryCount, "Query count must has text!");
    Assert.notNull(params, "QueryParams must not be null!");
    Assert.notNull(pageable, "PageRequest must not be null!");
    Assert.notNull(transformer, "Transformer must not be null!");

    Query query = em.createQuery(queryString);

    setQueryParams(query, params);
    query.setMaxResults(pageable.getPageSize());
    query.setFirstResult((int) pageable.getOffset());

    List<S> resultList = query.unwrap(QueryImpl.class).setResultTransformer(transformer).list();

    Query countQuery = em.createQuery(queryCount);
    setQueryParams(countQuery, params);
    Long total = (Long) countQuery.getSingleResult();

    Page<S> page = new PageImpl(resultList, pageable, total);

    return page;
}
 
Example #10
Source File: StandardItemFilterProcessorTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Tests uid query.
 * 
 * @throws Exception
 *             - if something is wrong this exception is thrown.
 */
@Test
public void testUidQuery() throws Exception {
    ItemFilter filter = new ItemFilter();
    filter.setUid(Restrictions.eq("abc"));
    QueryImpl<Item> query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibItem i where i.uid=:param0", query.getQueryString());
}
 
Example #11
Source File: StandardItemFilterProcessorTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
@Test
public void testModifiedSinceQuery() {
    NoteItemFilter filter = new NoteItemFilter();
    Calendar c = Calendar.getInstance();
    Date end = c.getTime();
    c.add(Calendar.YEAR, -1);
    filter.setModifiedSince(Restrictions.between(c.getTime(), end));
    QueryImpl<Item> query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibNoteItem i where i.modifiedDate between :param0 and :param1",
            query.getQueryString());
}
 
Example #12
Source File: StandardItemFilterProcessorTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Tests display name query.
 * 
 * @throws Exception
 *             - if something is wrong this exception is thrown.
 */
@Test
public void testDisplayNameQuery() throws Exception {
    ItemFilter filter = new ItemFilter();
    filter.setDisplayName(Restrictions.eq("test"));
    QueryImpl<Item> query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibItem i where i.displayName=:param0", query.getQueryString());

    filter.setDisplayName(Restrictions.neq("test"));
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibItem i where i.displayName!=:param0", query.getQueryString());

    filter.setDisplayName(Restrictions.like("test"));
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibItem i where i.displayName like :param0", query.getQueryString());

    filter.setDisplayName(Restrictions.nlike("test"));
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibItem i where i.displayName not like :param0", query.getQueryString());

    filter.setDisplayName(Restrictions.isNull());
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibItem i where i.displayName is null", query.getQueryString());

    filter.setDisplayName(Restrictions.ilike("test"));
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibItem i where lower(i.displayName) like :param0", query.getQueryString());

    filter.setDisplayName(Restrictions.nilike("test"));
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibItem i where lower(i.displayName) not like :param0",
            query.getQueryString());

}
 
Example #13
Source File: StandardItemFilterProcessorTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Tests parent query.
 * 
 * @throws Exception
 *             - if something is wrong this exception is thrown.
 */
@Test
public void testParentQuery() throws Exception {
    ItemFilter filter = new ItemFilter();
    CollectionItem parent = new HibCollectionItem();
    filter.setParent(parent);
    QueryImpl<Item> query = queryBuilder.buildQuery(filter);
    Assert.assertEquals(
            "select i from HibItem i join i.parentDetails pd where " + "pd.primaryKey.collection=:parent",
            query.getQueryString());
}
 
Example #14
Source File: StandardItemFilterProcessorTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Tests display name and parent query.
 * 
 * @throws Exception
 *             - if something is wrong this exception is thrown.
 */
@Test
public void testDisplayNameAndParentQuery() throws Exception {
    ItemFilter filter = new ItemFilter();
    CollectionItem parent = new HibCollectionItem();
    filter.setParent(parent);
    filter.setDisplayName(Restrictions.eq("test"));
    QueryImpl<Item> query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibItem i join i.parentDetails pd where "
            + "pd.primaryKey.collection=:parent and i.displayName=:param1", query.getQueryString());
}
 
Example #15
Source File: DefaultJpaRepository.java    From ueboot with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public <S> List<S> find(String queryString, NamedParams params, Class<S> transformer) {
    Assert.notNull(queryString, "Query must not be null!");
    Assert.notNull(params, "NamedParams must not be null!");
    Assert.notNull(transformer, "Transformer Class must not be null");

    Query query = em.createQuery(queryString);
    setQueryParams(query, params);
    return query.unwrap(QueryImpl.class).setResultTransformer(Transformers.aliasToBean(transformer)).list();
}
 
Example #16
Source File: StandardItemFilterProcessor.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Defined for testing reasons.
 */
protected QueryImpl<Item> buildQuery(ItemFilter filter) {
    return (QueryImpl<Item>) this.buildQueryInternal(filter);
}
 
Example #17
Source File: StandardItemFilterProcessorTest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Tests note item query.
 * 
 * @throws Exception
 *             - if something is wrong this exception is thrown.
 */
@Test
public void testNoteItemQuery() throws Exception {
    NoteItemFilter filter = new NoteItemFilter();
    CollectionItem parent = new HibCollectionItem();
    filter.setParent(parent);
    filter.setDisplayName(Restrictions.eq("test"));
    filter.setIcalUid(Restrictions.eq("icaluid"));
    filter.setBody(Restrictions.eq("body"));
    filter.setTriageStatusCode(Restrictions.eq(TriageStatus.CODE_DONE));

    QueryImpl<Item> query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibNoteItem i join i.parentDetails pd, "
            + "HibTextAttribute ta4 where pd.primaryKey.collection=:parent and "
            + "i.displayName=:param1 and i.triageStatus.code=:param2 and i.icalUid=:param3 and "
            + "ta4.item=i and ta4.qname=:ta4qname and ta4.value=:param5", query.getQueryString());

    filter = new NoteItemFilter();
    filter.setIsModification(true);
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibNoteItem i where i.modifies is not null", query.getQueryString());

    filter.setIsModification(false);
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibNoteItem i where i.modifies is null", query.getQueryString());

    filter.setIsModification(null);

    filter.setHasModifications(true);
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibNoteItem i where size(i.modifications) > 0", query.getQueryString());

    filter.setHasModifications(false);
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibNoteItem i where size(i.modifications) = 0", query.getQueryString());

    filter = new NoteItemFilter();
    filter.setMasterNoteItem(new HibNoteItem());
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals("select i from HibNoteItem i where (i=:masterItem or " + "i.modifies=:masterItem)",
            query.getQueryString());

    filter = new NoteItemFilter();
    Date date1 = new Date(1000);
    Date date2 = new Date(2000);
    filter.setReminderTime(Restrictions.between(date1, date2));
    query = queryBuilder.buildQuery(filter);
    Assert.assertEquals(
            "select i from HibNoteItem i, HibTimestampAttribute tsa0 where "
                    + "tsa0.item=i and tsa0.qname=:tsa0qname and tsa0.value between :param1 and :param2",
            query.getQueryString());

}