代码如下:
public List<Forum> replyForum(String forumId) {
log.info("");
try {
String sql = "from Forum as f where f.parentId=:forumId order by createTime";
Query query = getHibernateTemplate().getSessionFactory().openSession().createQuery(sql.toString());
query.setString("forumId",forumId);
List<Forum> list = query.list();
if (list != null && list.size() > 0) {
log.debug("");
log.info("");
return list;
} else {
log.debug("");
return null;
}
} catch (DataAccessException e) {
log.error("[ForumDaoHibernate]replyForum发生错误!\n"
+ FormatExceptionStack.formatException(e));
return null;
}
}
我想问的是getHibernateTemplate().getSessionFactory().openSession().createQuery(sql.toString());这中查询方式用的人多吗,对于大型的运营网站来说,这样的查询怎么样?好吗?是不是还有更好的办法,或者没有。再者getSessionFactory().openSession()到底要不要close,如果要关闭session的话,该怎么关闭??新手急,在线等待答案,谢谢!!!!~~~~(>_<)~~~~
public List<Forum> replyForum(String forumId) {
log.info("");
try {
String sql = "from Forum as f where f.parentId=:forumId order by createTime";
Query query = getHibernateTemplate().getSessionFactory().openSession().createQuery(sql.toString());
query.setString("forumId",forumId);
List<Forum> list = query.list();
if (list != null && list.size() > 0) {
log.debug("");
log.info("");
return list;
} else {
log.debug("");
return null;
}
} catch (DataAccessException e) {
log.error("[ForumDaoHibernate]replyForum发生错误!\n"
+ FormatExceptionStack.formatException(e));
return null;
}
}
我想问的是getHibernateTemplate().getSessionFactory().openSession().createQuery(sql.toString());这中查询方式用的人多吗,对于大型的运营网站来说,这样的查询怎么样?好吗?是不是还有更好的办法,或者没有。再者getSessionFactory().openSession()到底要不要close,如果要关闭session的话,该怎么关闭??新手急,在线等待答案,谢谢!!!!~~~~(>_<)~~~~
=====================================================================================
这条语句里我不知道你指的是哪部分的用法,getHibernateTemplate().getSessionFactory()获得SessionFactory。
SessionFactory是线程安全的,hibernate只会维护一个SessionFactory,所以你每次获得的都是同一个SessionFactory,
这样用不会造成额外的资源消耗,也就是不会重新创建一个SessionFactory。
session 不是线程安全的,所以用完后要关闭,调用session.close();就OK了
用完了当然要关闭了啊
SessionFactory sessions = new Configuration().configure().buildSessionFactory();
Session session = sessions.openSession();
Query query = session.createQuery("from Student");
List<?> list = query.list();
Iterator<?> it = list.iterator();
while(it.hasNext())
{
Student stu = (Student)it.next();
log.info("id: "+stu.getId()+" 姓名: "+stu.getSname()+" 学号: "+stu.getSno());
}
sessions.close();
看看这个
这样就关了
private static String ZL_HBM_XML = "/ZLSearch.hbm.xml";
SessionFactory sf = new Configuration().configure(ZL_HBM_XML).buildSessionFactory();
String queryString = "select SITE_NAME,CH_NAME from ODS_SITE@ODSCENTER order by CH_NAME";
Query queryObject = sf.openSession().createSQLQuery(queryString);
sf.close();
与系统默认的SessionFactory 相比是线程不安全的,要想办法塞到线程里面处理一下
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
楼主给点分啊,穷。