Search in sources :

Example 6 with Query

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

the class SellCarTest method testSellCar.

@Test
public void testSellCar() throws Exception {
    prepareData();
    Session session = openSession();
    Transaction tx = session.beginTransaction();
    Query query = session.createQuery("from Seller");
    Seller seller = (Seller) query.uniqueResult();
    assertNotNull(seller);
    assertEquals(1, seller.getBuyers().size());
    tx.commit();
    session.close();
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) Session(org.hibernate.Session) Test(org.junit.Test)

Example 7 with Query

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

the class AccountDAO method getTotalBalance.

public int getTotalBalance(AccountHolder holder, boolean useRegion) throws Exception {
    List results = (List) withTxSessionApply(useJta, sessionFactory, session -> {
        Query query = session.createQuery("select account.balance from Account as account where account.accountHolder = ?");
        query.setParameter(0, holder);
        if (useRegion) {
            query.setCacheRegion("AccountRegion");
        }
        query.setCacheable(true);
        return query.list();
    });
    int total = 0;
    if (results != null) {
        for (Iterator it = results.iterator(); it.hasNext(); ) {
            total += ((Integer) it.next()).intValue();
            System.out.println("Total = " + total);
        }
    }
    return total;
}
Also used : AccountHolder(org.hibernate.test.cache.infinispan.functional.entities.AccountHolder) List(java.util.List) Iterator(java.util.Iterator) TxUtil.withTxSession(org.hibernate.test.cache.infinispan.util.TxUtil.withTxSession) SessionFactory(org.hibernate.SessionFactory) Query(org.hibernate.Query) Account(org.hibernate.test.cache.infinispan.functional.entities.Account) InfinispanMessageLogger(org.hibernate.cache.infinispan.util.InfinispanMessageLogger) TxUtil.withTxSessionApply(org.hibernate.test.cache.infinispan.util.TxUtil.withTxSessionApply) Query(org.hibernate.Query) Iterator(java.util.Iterator) List(java.util.List)

Example 8 with Query

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

the class AccountDAO method getCountForBranch.

public int getCountForBranch(String branch, boolean useRegion) throws Exception {
    return withTxSessionApply(useJta, sessionFactory, session -> {
        Query query = session.createQuery("select account from Account as account where account.branch = :branch");
        query.setString("branch", branch);
        if (useRegion) {
            query.setCacheRegion("AccountRegion");
        }
        query.setCacheable(true);
        return query.list().size();
    });
}
Also used : Query(org.hibernate.Query)

Example 9 with Query

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

the class AccountDAO method internalCleanup.

private void internalCleanup() throws Exception {
    withTxSession(useJta, sessionFactory, session -> {
        Query query = session.createQuery("select account from Account as account");
        List accts = query.list();
        if (accts != null) {
            for (Iterator it = accts.iterator(); it.hasNext(); ) {
                try {
                    Object acct = it.next();
                    log.info("Removing " + acct);
                    session.delete(acct);
                } catch (Exception ignored) {
                }
            }
        }
    });
}
Also used : Query(org.hibernate.Query) Iterator(java.util.Iterator) List(java.util.List)

Example 10 with Query

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

the class AccountDAO method getBranch.

public String getBranch(Object holder, boolean useRegion) throws Exception {
    return withTxSessionApply(useJta, sessionFactory, session -> {
        Query query = session.createQuery("select account.branch from Account as account where account.accountHolder = ?");
        query.setParameter(0, holder);
        if (useRegion) {
            query.setCacheRegion("AccountRegion");
        }
        query.setCacheable(true);
        return (String) query.list().get(0);
    });
}
Also used : Query(org.hibernate.Query)

Aggregations

Query (org.hibernate.Query)481 Session (org.hibernate.Session)223 Test (org.junit.Test)184 List (java.util.List)85 Transaction (org.hibernate.Transaction)80 ArrayList (java.util.ArrayList)52 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)31 TestForIssue (org.hibernate.testing.TestForIssue)28 SQLQuery (org.hibernate.SQLQuery)27 MifosRuntimeException (org.mifos.core.MifosRuntimeException)24 PersistenceException (org.mifos.framework.exceptions.PersistenceException)22 HibernateException (org.hibernate.HibernateException)19 Collection (java.util.Collection)14 Date (java.util.Date)14 HashMap (java.util.HashMap)12 Iterator (java.util.Iterator)12 BigDecimal (java.math.BigDecimal)11 Map (java.util.Map)11 HibernateSearchException (org.mifos.framework.exceptions.HibernateSearchException)11 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)10