最近用STS搭建hibernate&spring,遇到Could not locate SessionFactory in JNDI.找到这个文章很快解决了

用HIBERNATE时出现如下错误:

ERROR - Could not locate SessionFactory in JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
 at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
 at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
 at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
 at javax.naming.InitialContext.lookup(InitialContext.java:351)
 at com.lmy.myblog.pojos.THostInfoHome.getSessionFactory(THostInfoHome.java:27)
 at com.lmy.myblog.pojos.THostInfoHome.<init>(THostInfoHome.java:22)
 at Test.main(Test.java:13)
Exception in thread "main" java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
 at com.lmy.myblog.pojos.THostInfoHome.getSessionFactory(THostInfoHome.java:30)
 at com.lmy.myblog.pojos.THostInfoHome.<init>(THostInfoHome.java:22)
 at Test.main(Test.java:13)

 

需要修改的两个地方:
    protected SessionFactory getSessionFactory() {
        try {
            //return (SessionFactory) new InitialContext().lookup("SessionFactory");这句不能通过,会提示 Could not locate SessionFactory in  JNDIjavax.naming.NoInitialContextException : Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
 
      修改为
       return (SessionFactory) new Configuration().configure().buildSessionFactory();
        }
        catch (Exception e) {
            log.error("Could not locate SessionFactory in JNDI", e);
            throw new IllegalStateException("Could not locate SessionFactory in JNDI"+e.toString());
        }
    }
 
 
 
    public Tb1 findById( java.lang.String id) {
        log.debug("getting Tb1 instance with id: " + id);
        try {
                  System.out.println("test0....."+sessionFactory);
//Tb1 instance= (Tb1)sessionFactory.getCurrentSession().get(“com.sss.common”,id);//这句有问题。会提示 No CurrentSessionContext configured!
               修改为如下两句
Session session = sessionFactory.openSession();//先加入import org.hibernate.Session;
            //Tb1 instance = (Tb1) session.load("com.sss.commmon.Tb1", id);这句也可以
               Tb1 instance = (Tb1) session.get("com.sss.commmon.Tb1", id);
              
            if (instance==null) {
                log.debug("get successful, no instance found");
            }
            else {
                log.debug("get successful, instance found");
            }
            return instance;
        }
        catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }


非常感谢作者!
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值