62,612
社区成员
发帖
与我相关
我的任务
分享
@Test
public void testIterate() {
Session s = sf.getCurrentSession();
s.beginTransaction();
Query q = s.createQuery("from Category");
Iterator<Category> it = q.iterate();
while (it.hasNext()) {
Category c = it.next();
System.out.println(c.getId());
}
it = q.iterate();
while (it.hasNext()) {
Category c = it.next();
System.out.println(c.getId());
}
s.getTransaction().commit();
}
public static List queryList(String hql) {
Session session = null;
Transaction tx = null;
List result=null;
try {
session = getCurrentSession();
tx = session.beginTransaction();
Query query=session.createQuery(hql);
result = query.list();
query.list();
Iterator iterator=query.iterate();
if(iterator.hasNext()){
((Event)iterator.next()).getTitle();
}
tx.commit();
} catch (Exception e) {
try {
tx.rollback();
} catch (HibernateException e1) {
logger.error("transaction rollback failed", e);
}
logger.error("data insert failed", e);
} finally {
closeCurrentSession();
}
return result;
}
public static List queryList(String hql) {
Session session = null;
Transaction tx = null;
List result=null;
try {
session = getCurrentSession();
tx = session.beginTransaction();
Query query=session.createQuery(hql);
/*result = query.list();
query.list();*/
Iterator iterator=query.iterate();
if(iterator.hasNext()){
((Event)iterator.next()).getTitle();
}
tx.commit();
} catch (Exception e) {
try {
tx.rollback();
} catch (HibernateException e1) {
logger.error("transaction rollback failed", e);
}
logger.error("data insert failed", e);
} finally {
closeCurrentSession();
}
return result;
}
@Test
public void testCache3() {
Session s = sf.getCurrentSession();
s.beginTransaction();
Iterator<Category> it1 = s.createQuery("from Category").setCacheable(true).iterate();
while (it1.hasNext()) {
Category c = it1.next();
System.out.println(c.getId());
}
Iterator<Category> it2 = s.createQuery("from Category").setCacheable(true).iterate();
s.getTransaction().commit();
}