Search in sources :

Example 46 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class NaturalIdInvalidationTest method getState.

private State getState(Session s, String name) {
    Criteria criteria = s.createCriteria(State.class);
    criteria.add(Restrictions.eq("name", name));
    criteria.setCacheable(true);
    return (State) criteria.list().get(0);
}
Also used : State(org.hibernate.test.cache.infinispan.functional.entities.State) Criteria(org.hibernate.Criteria)

Example 47 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class NaturalIdInvalidationTest method deleteCitizenWithCriteria.

private void deleteCitizenWithCriteria(SessionFactory sf) throws Exception {
    withTxSession(sf, s -> {
        State france = getState(s, "Ile de France");
        Criteria criteria = s.createCriteria(Citizen.class);
        criteria.add(Restrictions.naturalId().set("ssn", "1234").set("state", france));
        criteria.setCacheable(true);
        Citizen c = (Citizen) criteria.uniqueResult();
        s.delete(c);
    });
}
Also used : State(org.hibernate.test.cache.infinispan.functional.entities.State) Citizen(org.hibernate.test.cache.infinispan.functional.entities.Citizen) Criteria(org.hibernate.Criteria)

Example 48 with Criteria

use of org.hibernate.Criteria in project head by mifos.

the class LegacyLoanDao method getTotalOutstandingPrincipalOfLoanAccountsInActiveGoodStanding.

@SuppressWarnings("unchecked")
public BigDecimal getTotalOutstandingPrincipalOfLoanAccountsInActiveGoodStanding(final Short branchId, final Short loanOfficerId, final Short loanProductId) throws PersistenceException {
    BigDecimal loanBalanceAmount = new BigDecimal(0);
    try {
        Session session = StaticHibernateUtil.getSessionTL();
        Criteria criteria = session.createCriteria(LoanBO.class).setProjection(Projections.sum("loanBalance.amount")).add(Restrictions.eq("accountState.id", (short) 5)).add(Restrictions.eq("office.officeId", branchId));
        if (loanOfficerId != (short) -1) {
            criteria.add(Restrictions.eq("personnel.personnelId", loanOfficerId));
        }
        if (loanProductId != (short) -1) {
            criteria.add(Restrictions.eq("loanOffering.prdOfferingId", loanProductId));
        }
        List list = criteria.list();
        loanBalanceAmount = (BigDecimal) list.get(0);
    } catch (Exception e) {
        throw new PersistenceException(e);
    }
    return loanBalanceAmount;
}
Also used : LoanBO(org.mifos.accounts.loan.business.LoanBO) PersistenceException(org.mifos.framework.exceptions.PersistenceException) ArrayList(java.util.ArrayList) List(java.util.List) Criteria(org.hibernate.Criteria) BigDecimal(java.math.BigDecimal) InvalidDateException(org.mifos.application.admin.servicefacade.InvalidDateException) MifosRuntimeException(org.mifos.core.MifosRuntimeException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session)

Example 49 with Criteria

use of org.hibernate.Criteria in project head by mifos.

the class PersonnelDaoHibernate method search.

@SuppressWarnings("unchecked")
@Override
public SystemUserSearchResultsDto search(UserSearchDto searchDto, MifosUser user) {
    Short userId = Integer.valueOf(user.getUserId()).shortValue();
    PersonnelBO loggedInUser = findPersonnelById(userId);
    final PersonnelLevel level = loggedInUser.getLevelEnum();
    final String searchAllSubOfficesInclusiveOfLoggedInUserOffice = loggedInUser.getOfficeSearchId() + "%";
    final String searchString = org.mifos.framework.util.helpers.SearchUtils.normalizeSearchString(searchDto.getSearchTerm());
    final String username = searchString + "%";
    String firstName = "";
    String secondPartOfName = "";
    HashMap<String, Object> queryParameters = new HashMap<String, Object>();
    queryParameters.put("SEARCH_ALL", searchAllSubOfficesInclusiveOfLoggedInUserOffice);
    queryParameters.put("USERID", userId);
    queryParameters.put("LOID", PersonnelLevel.LOAN_OFFICER.getValue());
    queryParameters.put("USERLEVEL_ID", level.getValue());
    queryParameters.put("USER_NAME", username);
    if (searchString.contains(" ")) {
        firstName = searchString.substring(0, searchString.indexOf(" "));
        secondPartOfName = searchString.substring(searchString.indexOf(" ") + 1, searchString.length());
        queryParameters.put("USER_NAME1", firstName);
        queryParameters.put("USER_NAME2", secondPartOfName);
    } else {
        firstName = searchString;
        secondPartOfName = "";
        queryParameters.put("USER_NAME1", searchString);
        queryParameters.put("USER_NAME2", "");
    }
    Long searchResultsCount = (Long) this.genericDao.executeUniqueResultNamedQuery(NamedQueryConstants.PERSONNEL_SEARCH_COUNT, queryParameters);
    Session session = StaticHibernateUtil.getSessionTL();
    Criteria criteriaQuery = session.createCriteria(PersonnelBO.class);
    criteriaQuery.createAlias("office", "o");
    criteriaQuery.createAlias("personnelDetails", "d");
    if (PersonnelLevel.LOAN_OFFICER.getValue().equals(Short.valueOf("2"))) {
        criteriaQuery.add(Restrictions.eq("personnelId", userId));
    }
    criteriaQuery.add(Restrictions.like("o.searchId", searchAllSubOfficesInclusiveOfLoggedInUserOffice));
    LogicalExpression firstOrLastNameMatchUsername = Restrictions.or(Restrictions.like("d.name.firstName", username), Restrictions.like("d.name.lastName", username));
    LogicalExpression firstNameAndLastNameMatchGivenParts = Restrictions.and(Restrictions.like("d.name.firstName", firstName), Restrictions.like("d.name.lastName", secondPartOfName));
    criteriaQuery.add(Restrictions.or(firstOrLastNameMatchUsername, firstNameAndLastNameMatchGivenParts));
    criteriaQuery.addOrder(Order.asc("o.officeName"));
    criteriaQuery.addOrder(Order.asc("d.name.lastName"));
    criteriaQuery.setFetchMode("office", FetchMode.JOIN);
    criteriaQuery.setFetchMode("level", FetchMode.JOIN);
    criteriaQuery.setFetchMode("personnelDetails", FetchMode.JOIN);
    int firstResult = (searchDto.getPage() * searchDto.getPageSize()) - searchDto.getPageSize();
    criteriaQuery.setFirstResult(firstResult);
    criteriaQuery.setMaxResults(searchDto.getPageSize());
    List<PersonnelBO> pagedResults = criteriaQuery.list();
    List<UserDetailDto> pagedUserDetails = new ArrayList<UserDetailDto>();
    for (PersonnelBO personnelBO : pagedResults) {
        pagedUserDetails.add(personnelBO.toDto());
    }
    SystemUserSearchResultsDto resultsDto = new SystemUserSearchResultsDto(searchResultsCount.intValue(), firstResult, searchDto.getPage(), searchDto.getPageSize(), pagedUserDetails);
    return resultsDto;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Criteria(org.hibernate.Criteria) LogicalExpression(org.hibernate.criterion.LogicalExpression) PersonnelBO(org.mifos.customers.personnel.business.PersonnelBO) UserDetailDto(org.mifos.dto.domain.UserDetailDto) PersonnelLevel(org.mifos.customers.personnel.util.helpers.PersonnelLevel) SystemUserSearchResultsDto(org.mifos.dto.screen.SystemUserSearchResultsDto) Session(org.hibernate.Session)

Example 50 with Criteria

use of org.hibernate.Criteria in project hibernate-orm by hibernate.

the class ReadWriteTest method testNaturalIdCached.

@Test
public void testNaturalIdCached() throws Exception {
    saveSomeCitizens();
    // Clear the cache before the transaction begins
    cleanupCache();
    TIME_SERVICE.advance(1);
    withTxSession(s -> {
        State france = ReadWriteTest.this.getState(s, "Ile de France");
        Criteria criteria = s.createCriteria(Citizen.class);
        criteria.add(Restrictions.naturalId().set("ssn", "1234").set("state", france));
        criteria.setCacheable(true);
        Statistics stats = sessionFactory().getStatistics();
        stats.setStatisticsEnabled(true);
        stats.clear();
        assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
        List results = criteria.list();
        assertEquals(1, results.size());
        assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount());
        criteria.list();
        assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
        assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
        assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
        assertEquals("NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount());
        markRollbackOnly(s);
    });
}
Also used : State(org.hibernate.test.cache.infinispan.functional.entities.State) ArrayList(java.util.ArrayList) List(java.util.List) Criteria(org.hibernate.Criteria) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test)

Aggregations

Criteria (org.hibernate.Criteria)180 Session (org.hibernate.Session)95 Test (org.junit.Test)69 List (java.util.List)39 Transaction (org.hibernate.Transaction)39 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)27 ArrayList (java.util.ArrayList)15 TestForIssue (org.hibernate.testing.TestForIssue)12 Iterator (java.util.Iterator)9 Period (org.hisp.dhis.period.Period)8 Map (java.util.Map)6 State (org.hibernate.test.cache.infinispan.functional.entities.State)5 OnmsCriteria (org.opennms.netmgt.model.OnmsCriteria)5 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)5 HashSet (java.util.HashSet)3 Criterion (org.hibernate.criterion.Criterion)3 Statistics (org.hibernate.stat.Statistics)3 Pager (org.hisp.dhis.common.Pager)3 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2 HashMap (java.util.HashMap)2