Hibernate ORM Criteria - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Hibernate ORM Criteria class.

This code example shows how to use the following methods:add, createAlias, setProjection, setResultTransformer
    } 
     
    /** 
     * Test. 
     */ 
    @Test 
    public void testGetCriteria() 
    { 
        context.checking(new Expectations() 
        { 
            { 
                oneOf(hibernateSession).createCriteria(DomainGroup.class); 
            } 
        }); 
 
        Criteria result = sut.getCriteria(hibernateSession); 
        assertNotNull(result); 
         
        context.assertIsSatisfied(); 
    } 
} 
8
CodeRank
This code example shows how to use the following methods:add, createAlias, setProjection, setResultTransformer
        fields.add(getColumn("stickyActivityId"));
        fields.add(Projections.property("cb.accountId").as("personCreatedByAccountId"));
        fields.add(Projections.property("cb.displayName").as("personCreatedByDisplayName"));
        fields.add(Projections.property("cb.companyName").as("personCreatedByCompanyName"));
        fields.add(Projections.property("stream.id").as("streamId"));
        criteria.setProjection(fields);
        criteria.createAlias("createdBy", "cb");
        criteria.createAlias("streamScope", "stream");
 
        // We don't currently cache pending groups 
        // TODO: this needs to change - we should figure out how to remove this rule 
        criteria.add(Restrictions.eq("isPending", false));
 
        ModelViewResultTransformer<DomainGroupModelView> resultTransformer = //
        new ModelViewResultTransformer<DomainGroupModelView>(new DomainGroupModelViewFactory()); 
        criteria.setResultTransformer(resultTransformer);
 
        return criteria;
    } 
 
    /** 
8
CodeRank
This code example shows how to use the following methods:list, setFirstResult, setMaxResults, setProjection, setResultTransformer
		return list;
	} 
	 
	@Override 
	public ListPart<T> listPart(Long firstResult, Long maxResults) {
		criteria.setProjection(null);
		criteria.setResultTransformer(Criteria.ROOT_ENTITY);
		 
		if (firstResult != null) {
			criteria.setFirstResult(firstResult.intValue());
		} 
		 
		if (maxResults != null) {
			criteria.setMaxResults(maxResults.intValue());
		} 
		 
		@SuppressWarnings("unchecked") 
		List<T> list = criteria.list();
		 
		return ListPart.newListPart(list, firstResult, maxResults, null, null);

7
CodeRank
This code example shows how to use the following methods:createAlias, setProjection, setResultTransformer
            } 
        }); 
 
        Criteria result = sut.getCriteria(hibernateSession); 
        assertNotNull(result); 
 
        context.assertIsSatisfied(); 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:list, setFirstResult, setMaxResults, setProjection, setResultTransformer
		} catch (Exception e) {
			throw new RuntimeException(
					"cannot read/write 'orderEntries' from CriteriaImpl", e);
		} 
 
		int totalCount = ((Number) crit.setProjection(Projections.rowCount())
				.uniqueResult()).intValue();
		Pagination p = new Pagination(pageNo, pageSize, totalCount);
		if (totalCount < 1) {
			p.setList(new ArrayList());
			return p;
		} 
 
		// 将之前的Projection,ResultTransformer和OrderBy条件重新设回去 
		crit.setProjection(projection);
		if (projection == null) {
			crit.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
		} 
		if (transformer != null) {
			crit.setResultTransformer(transformer);

7
CodeRank
This code example shows how to use the following methods:list
	} 
 
	@Override 
	@SuppressWarnings("rawtypes") 
	public List list(boolean unmodifiable) {
		List list = _criteria.list();
 
		if (unmodifiable) {
			return Collections.unmodifiableList(list);
		} 
		else { 
			return ListUtil.copy(list);
		} 
	} 
 
	@Override 
	public void setLimit(int start, int end) {
		_start = Integer.valueOf(start);
		_end = Integer.valueOf(end);
	} 
7
CodeRank
This code example shows how to use the following methods:createAlias, setProjection, setResultTransformer
        fields.add(getColumn("workPhone"));
        fields.add(getColumn("cellPhone"));
        fields.add(getColumn("fax"));
        fields.add(Projections.property("streamViewHiddenLineIndex").as("compositeStreamHiddenLineIndex"));
        fields.add(Projections.property("stream.id").as("streamId"));
        criteria.setProjection(fields);
        criteria.createAlias("streamScope", "stream");
 
        ModelViewResultTransformer<PersonModelView> resultTransformer = new ModelViewResultTransformer<PersonModelView>(
                new PersonModelViewFactory()); 
        criteria.setResultTransformer(resultTransformer);
 
        return criteria;
    } 
 
    /** 
     * Build the PropertyProjection with alias. 
     *  
     * @param propertyName 
     *            the property name 
7
CodeRank
This code example shows how to use the following methods:setTimeout
	 * @param criteria the Hibernate Criteria object 
	 * @param sessionFactory Hibernate SessionFactory that the Criteria was created for 
	 * @see org.hibernate.Criteria#setTimeout 
	 */ 
	public static void applyTransactionTimeout(Criteria criteria, SessionFactory sessionFactory) { 
		Assert.notNull(criteria, "No Criteria object specified"); 
		if (sessionFactory != null) { 
			SessionHolder sessionHolder = 
				(SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory); 
			if (sessionHolder != null && sessionHolder.hasTimeout()) { 
				criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds()); 
			} 
		} 
	} 
 
	/** 
	 * Convert the given HibernateException to an appropriate exception 
	 * from the {@code org.springframework.dao} hierarchy. 
	 * @param ex HibernateException that occurred 
	 * @return the corresponding DataAccessException instance 
6
CodeRank
This code example shows how to use the following methods:list
 
        List<XWikiDeletedDocument> deletedVersions =
            Arrays.asList(mock(XWikiDeletedDocument.class, "v1"), mock(XWikiDeletedDocument.class, "v2"));
 
        Criteria criteria = mock(Criteria.class);
        when(criteria.list()).thenReturn(deletedVersions);
        when(session.createCriteria(XWikiDeletedDocument.class)).thenReturn(criteria);
 
        assertArrayEquals(deletedVersions.toArray(new XWikiDeletedDocument[2]), mocker.getComponentUnderTest()
            .getAllDeletedDocuments(document, context, true));
 
        // Too bad the restrictions don't implement equals.. 
        verify(criteria).add(argThat(new HasToString<SimpleExpression>(equalTo("fullName=Space.Page"))));
        verify(criteria).add(argThat(new HasToString<SimpleExpression>(equalTo("language=ro"))));
        verify(criteria).addOrder(argThat(new HasToString<Order>(equalTo("date desc"))));
    } 
 
    @Test
    public void getAllDeletedDocumentsWhenLanguageIsEmpty() throws Exception
6
CodeRank
This code example shows how to use the following methods:addOrder, list
    @SuppressWarnings("unchecked") 
    public List<Member> findAllOrderedByName() {
        // using Hibernate Session and Criteria Query via Hibernate Native API 
        Session session = (Session) em.getDelegate();
        Criteria cb = session.createCriteria(Member.class);
        cb.addOrder(Order.asc("name"));
        return (List<Member>) cb.list();
        // return members; 
    } 
} 
7
CodeRank
See Code Examples for Hibernate ORM Criteria Methods: