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();
}
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;
}
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();
});
}
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) {
}
}
}
});
}
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);
});
}
Aggregations