hibernate查询问题 list()方法有时卡住不执行 具体情况内详

Imore 2008-05-30 07:11:30
我是用struts+hibernate写的一个东西

里面一处地方用到AJAX技术

用AJAX执行一个删除工作经历

然后再去重新获取工作经历列表的时候

query.list()方法就失效了

卡在那里执行不下去

但是于工作经历逻辑完全相同的教育经历就没问题

求达人解答
...全文
421 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Imore 2008-05-30
  • 打赏
  • 举报
回复
DAO方法中换成
this.session = SessionFactory.getSession();
再试试?
那个类我直接拿来用就是可以的吧?
Shine_Panda 2008-05-30
  • 打赏
  • 举报
回复
query.list()方法就失效了
可以能是由于你创建了太多 SessionFactory导致的 建议先改一下 获得Sesssion 的方式后再调试。
就是我 5楼提供的方法。
favorite7w 2008-05-30
  • 打赏
  • 举报
回复
参考HibernateUtil的使用。
http://dev.21tx.com/2005/09/20/15730.html

好像跑题了,呵呵..
Shine_Panda 2008-05-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Imore 的回复:]
我对处理session的打开和关闭一直没一个明确的思想

因为有时候需要几个不同的DAO对象一起使用

所以session需要在方法结束时候关闭掉

但是再用的时候为了不是关闭的状态于是就每次都打开 每次都关闭了……

请问正确的设计该是怎么做的呢
[/Quote]
真取的做法是每个业务需求一个session .
比如:做 添加时一个session . 删除时候一个session 。因为session是带缓存的所以不应该将一个session 来完成不同的操作
Shine_Panda 2008-05-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 Imore 的回复:]

每个都有……
正确的习惯该是怎么样的呢
[/Quote]
可以用工具生成的这个类。也可以直接写一个单例工厂。来获得session

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {

/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;

private HibernateSessionFactory() {
}

/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/


public static Session getAnotherSession() throws HibernateException{
if (sessionFactory == null) {
rebuildSessionFactory();
}
return sessionFactory.openSession();

}



public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}

/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}

/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}


public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}

/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}

Imore 2008-05-30
  • 打赏
  • 举报
回复
我对处理session的打开和关闭一直没一个明确的思想

因为有时候需要几个不同的DAO对象一起使用

所以session需要在方法结束时候关闭掉

但是再用的时候为了不是关闭的状态于是就每次都打开 每次都关闭了……

请问正确的设计该是怎么做的呢
Imore 2008-05-30
  • 打赏
  • 举报
回复

每个都有……
正确的习惯该是怎么样的呢
favorite7w 2008-05-30
  • 打赏
  • 举报
回复
你每个DAO方法都有这样的代码吗?这可不是个好习惯哟。
Configuration config = new Configuration().configure();
SessionFactory sf = config.buildSessionFactory();
Imore 2008-05-30
  • 打赏
  • 举报
回复
删除工作经历的DAO方法
public void deleteEmpWork(int WorkId) {
String hql = "Delete From Emp_Work Where ID=?";
Query q = null;
Configuration config = new Configuration().configure();
SessionFactory sf = config.buildSessionFactory();
this.session = sf.openSession();
q = this.session.createQuery(hql);
q.setInteger(0, WorkId);
q.executeUpdate();
this.session.close();
}

获取工作经历列表的方法
public ArrayList<Emp_Work> getEmpWorkByCondition(String empId) {
ArrayList<Emp_Work> work_list = null;
Query q = null;
String hql = "From Emp_Work Where empId=?";
Configuration config = new Configuration().configure();
SessionFactory sf = config.buildSessionFactory();
this.session = sf.openSession();
q = this.session.createQuery(hql);
q.setString(0, empId);
System.out.println("333");
work_list = (ArrayList<Emp_Work>) q.list();
System.out.println("444");
session.close();
return work_list;
}

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧