在页面调用hibernate sessionfactory.opensession()没有反应?

xingwei4225 2009-07-24 11:32:43
hibernate 配置好后,在后台java代码中调用,修改和查询都没有问题,在是他jsp页面调用查询方法时,报空指针异常,查看原因,是因为执行到session=sessionfactory.opensession()这句没有反应,请问这是为什么?
找了好长时间都不知道为什么,望指教!
...全文
201 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingwei4225 2009-07-27
  • 打赏
  • 举报
回复
谢谢各位,问题已经解决! 问题好像是少了两个包
我的创建session的代码是这样的:

public class HibernateUtils {
private static final SessionFactory sessionfactory;
static {
try {
sessionfactory = new Configuration().configure()
.buildSessionFactory();
System.out.println("Initial SessionFactory creation ok.");
System.out.println(sessionfactory);
} catch (Throwable ex) {
ex.printStackTrace();
System.out.println("Initial SessionFactory creation failed.");
throw new ExceptionInInitializerError(ex);
}
}

public static final ThreadLocal tLocalsess = new ThreadLocal();
public static final ThreadLocal tLocaltx = new ThreadLocal();

// 取得session
public static Session getSession() {
Session session = (Session) tLocalsess.get();
// 打开一个新的session,如果当前的不可用
try {
if (session == null || !session.isOpen()) {

session = openSession();
tLocalsess.set(session);
}
} catch (HibernateException e) {
e.printStackTrace();
}
return session;
}

public static void closeSession() {
Session session = (Session) tLocalsess.get();
tLocalsess.set(null);
try {
if (session != null && session.isOpen()) {
session.close();
}
} catch (HibernateException e) {
e.printStackTrace();
}
}

public static void beginTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
try {
if (tx == null) {
tx = getSession().beginTransaction();
tLocaltx.set(tx);
}
} catch (HibernateException e) {
e.printStackTrace();
}
}

public static void commitTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()){
tx.commit();
}

} catch (HibernateException e) {
e.printStackTrace();
}
}

public static void rollbackTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
try {
tLocaltx.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.rollback();
}
} catch (HibernateException e) {
e.printStackTrace();
}
}

private static Session openSession() throws HibernateException {
return getSessionFactory().openSession();
}

private static SessionFactory getSessionFactory() throws HibernateException {
return sessionfactory;
}
}

bean 2009-07-25
  • 打赏
  • 举报
回复
sessionfactory没有创建好吧,给你一个工具类:

package com.bjsxt.hibernate;

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

public class HibernateUtils {

private static SessionFactory factory;

static {
try {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}catch(Exception e) {
e.printStackTrace();
}
}

public static SessionFactory getSessionFactory() {
return factory;
}

public static Session getSession() {
return factory.openSession();
}

public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
}


myeclipse能自动生成一个封装类,你可以用那个,
BearKin 2009-07-25
  • 打赏
  • 举报
回复
具体是哪个对象为空啊。。。
pengtaiwei 2009-07-25
  • 打赏
  • 举报
回复
在添加hibernate的时候,MyEclipse会自动生成一个文件,里面封装了获取session的方法。
qq707472 2009-07-25
  • 打赏
  • 举报
回复
你要知道jsp有个内置对象也是session , 请你先明确你调用的session 比如用的时候加上 org.hibernate.Session
zy0402 2009-07-25
  • 打赏
  • 举报
回复
自动生成dao就可以了
connor_zheng 2009-07-25
  • 打赏
  • 举报
回复
你用struts的 话,在action里查询出来,request.setAttribute(“别名”,查出来的结果)。
然后在jsp页面上用EL表达式取出来就ok了。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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