Search in sources :

Example 6 with Criteria

use of org.hibernate.Criteria in project java-design-patterns by iluwatar.

the class WizardDaoImpl method findByName.

@Override
public Wizard findByName(String name) {
    Session session = getSession();
    Transaction tx = null;
    Wizard result = null;
    try {
        tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(persistentClass);
        criteria.add(Restrictions.eq("name", name));
        result = (Wizard) criteria.uniqueResult();
        for (Spellbook s : result.getSpellbooks()) {
            s.getSpells().size();
        }
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        throw e;
    } finally {
        session.close();
    }
    return result;
}
Also used : Transaction(org.hibernate.Transaction) Spellbook(com.iluwatar.servicelayer.spellbook.Spellbook) Criteria(org.hibernate.Criteria) Session(org.hibernate.Session)

Example 7 with Criteria

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

the class NaturalIdInvalidationTest method getCitizenWithCriteria.

private void getCitizenWithCriteria(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);
        criteria.list();
    });
}
Also used : State(org.hibernate.test.cache.infinispan.functional.entities.State) Criteria(org.hibernate.Criteria)

Example 8 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 9 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 10 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)

Aggregations

Criteria (org.hibernate.Criteria)174 Session (org.hibernate.Session)92 Test (org.junit.Test)69 Transaction (org.hibernate.Transaction)39 List (java.util.List)35 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)4 HibernateCallback (org.springframework.orm.hibernate3.HibernateCallback)4 HashSet (java.util.HashSet)3 Criterion (org.hibernate.criterion.Criterion)3 Example (org.hibernate.criterion.Example)3 Statistics (org.hibernate.stat.Statistics)3 Pager (org.hisp.dhis.common.Pager)3 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2