为什么是空指针?

Code_xie 2007-10-26 08:15:58
在myclipse写的:有个异常
java.lang.NullPointerException
mypack.HibernateDAO.getCurrentSession(HibernateDAO.java:7)


相应的代码如下:public Session getCurrentSession(){
return HibernateSessionFactory.getSessionFactory().getCurrentSession();}

顺便问下myeclipse中,自动生成的SessionFactory 要不要重写。
...全文
132 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yztommyhc 2007-10-27
  • 打赏
  • 举报
回复
重复的问题不要发那么多次,谢谢!
yztommyhc 2007-10-27
  • 打赏
  • 举报
回复
HibernateSessionFactory.getSession()用这个方法。

myEclipse生成的这个HibernateSessionFactory类当然不用重写,直接调用

package cn.nuaa.service;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import hbm.HibernateSessionFactory;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import cn.nuaa.dao.TestDAO;
import cn.nuaa.vo.Event;
import cn.nuaa.vo.Speaker;


public class TestService
{

public static void main(String[] args) throws UnsupportedEncodingException
{
Event e = new Event();
Speaker s1 = new Speaker() ;
Speaker s2 = new Speaker() ;
s1.setName("111") ;
s2.setName("222") ;
s1.setEvent(e) ;
s2.setEvent(e) ;
e.setTopic("java") ;

e.getSpeakers().add(s1) ;
e.getSpeakers().add(s2) ;

Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();




session.save(e) ;
tx.commit();
session.close();

}

}
参考这个代码,用的就是myEclipse自动生成的HibernateSessionFactory这个类。
xiyuan1999 2007-10-26
  • 打赏
  • 举报
回复
没有取到Session的值
lihaifeng0412 2007-10-26
  • 打赏
  • 举报
回复
haha
Code_xie 2007-10-26
  • 打赏
  • 举报
回复
package mypack;
import java.util.List;
import org.hibernate.Session;
import mypack.HibernateSessionFactory;
public class HibernateDAO {
public Session getCurrentSession(){
return HibernateSessionFactory.getSessionFactory().openSession();}//错误处。

public void saveObject(Object ob)
{getCurrentSession().save(ob);}

public void updateObject(Object ob)
{getCurrentSession().update(ob);}

public void deleteObject(Object ob)
{getCurrentSession().delete(ob);}

public List getObjects(String hsql){
List result=getCurrentSession().createQuery(hsql).list();
return result;
}

public Object getObject(String hsql){
Object result=getCurrentSession().createQuery(hsql).uniqueResult();
return result;
}

public Object getObject(Class cls,int id){
Object result=getCurrentSession().get(cls, id);
return result;
}
}
Code_xie 2007-10-26
  • 打赏
  • 举报
回复
package mypack;

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


public class HibernateSessionFactory {


private static String CONFIG_FILE_LOCATION = "/mypack/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;

static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}


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;
}


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


public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

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


public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}


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


public static Configuration getConfiguration() {
return configuration;
}

}

67,516

社区成员

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

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