获取严重性:在JNDI中,无法找到SessionFactory,同时会话sessionfactory [英] Getting SEVERE: Could not locate SessionFactory in JNDI while geeting sessionfactory

查看:355
本文介绍了获取严重性:在JNDI中,无法找到SessionFactory,同时会话sessionfactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用hibernate作为持久层与数据库进行通信。我使用maven插件hbm2java生成dao,java,* hbm.xml和hibernate.cfg.xml。直到那个时候插件工作正常。



但是当我尝试与db通信时,它会提供以下异常。

  2012年8月30日1:45:46 PM org.hbm2dao.AssemblyHome getSessionFactory 
SEVERE:找不到JNDI中的SessionFactory
javax.naming.NoInitialContextException:需要指定环境或系统属性中的类名称,或作为applet参数,或应用程序资源文件中的java.naming.factory.initial
javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
在javax.naming.InitialContext.getDefaultInitCtx(未知源)
在javax.naming.InitialContext.getURLOrDefaultInitCtx(未知源)
在javax.naming.InitialContext.lookup(未知来源)
在org .hbm2dao.AssemblyHome.getSessionFactory(AssemblyHome.java:29)
在org.hbm2dao.AssemblyHome。< init>(AssemblyHome.java:24)
在com.myhadoop.app.App.main( App.java:22)
java.lang.IllegalStateException:找不到SessionFa在org.hbm2dao.AssemblyHome.getSessionFactory(AssemblyHome.java:33)中的JNDI
中的
org.hbm2dao.AssemblyHome中的
< init>(AssemblyHome.java:24)
at com .myhadoop.app.App.main(App.java:22)
线程main中的异常java.lang.NullPointerException
在com.myhadoop.app.App.main(App.java:32 )

默认情况下hibernate3:hbm2cfgxml生成具有会话工厂名称为SessionFactory的hibernate.cfg.xml文件。就像下面这样。

 < session-factory name =SessionFactory> 

插件生成以下代码以获取会话工厂。



session $ {pre> private final SessionFactory sessionFactory = getSessionFactory();

protected SessionFactory getSessionFactory(){
try {

return(SessionFactory)new InitialContext()。lookup(SessionFactory);
}
catch(异常e){
log.error(在JNDI中找不到SessionFactory,e);
抛出新的IllegalStateException(在JNDI中找不到SessionFactory);
}
}

我发现类似的查询使用eclipse Hibernate插件的问题 - 无法在JNDI中找到sessionfactory



但是,改变插件生成的400多个类,从* Home.java获取会话工厂是不可行的。



如何在不改变400+类的情况下摆脱这个hibernate插件的这个问题?



我不想使用任何网络/应用服务器这是我的独立应用程序。

解决方案

使用生成的 getSessionFactory()方法仅在您设置环境使 InitialContext 可以找到你的 hibernate.cfg.xml 文件,这听起来像是不能。



您可以通过根据 InitialContext 和您的环境来解决该问题。 api / javax / naming / InitialContext.htmlrel =nofollow> InitialContext javadocs 。或者,您可以通过更改 getSessionFactory 方法来简单地创建一个 配置 ,并告诉它映射文件的位置是。


I am using hibernate as persistence layer to communicate with the database. I used maven plugin hbm2java to generate dao, java, *hbm.xml and hibernate.cfg.xml. till that time plugins works fine.

But when I trying to communicate to db it gives following exception.

Aug 30, 2012 1:45:46 PM org.hbm2dao.AssemblyHome getSessionFactory
SEVERE: 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(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at org.hbm2dao.AssemblyHome.getSessionFactory(AssemblyHome.java:29)
at org.hbm2dao.AssemblyHome.<init>(AssemblyHome.java:24)
at com.myhadoop.app.App.main(App.java:22)
java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
at org.hbm2dao.AssemblyHome.getSessionFactory(AssemblyHome.java:33)
at org.hbm2dao.AssemblyHome.<init>(AssemblyHome.java:24)
at com.myhadoop.app.App.main(App.java:22)
Exception in thread "main" java.lang.NullPointerException
at com.myhadoop.app.App.main(App.java:32)

by default hibernate3:hbm2cfgxml generates hibernate.cfg.xml file with session-factory name as SessionFactory. just like following.

<session-factory name="SessionFactory">

plugin generate following code to get session factory.

private final SessionFactory sessionFactory = getSessionFactory();

protected SessionFactory getSessionFactory() {
    try {

        return (SessionFactory) new InitialContext().lookup("SessionFactory");
    }
    catch (Exception e) {
        log.error("Could not locate SessionFactory in JNDI", e);
        throw new IllegalStateException("Could not locate SessionFactory in JNDI");
    }
}

I found similar query Problems using eclipse Hibernate plugin - could not locate sessionfactory in JNDI.

However it not feasible to change 400+ classes generated by plugin to get session factory from *Home.java.

How to get rid of this problem with the hibernate plugin without changing 400+ classes?

I do not want to use any web/app server this is my standalone application.

解决方案

Using that generated getSessionFactory() method will only work if you have set up your environment such that InitialContext can find your hibernate.cfg.xml file, and it sounds like it can't.

You can fix that problem by setting up InitialContext and your environment correctly as per the InitialContext javadocs. As an alternative, you can cut out JNDI entirely by changing the getSessionFactory method to simply create a Configuration, and telling it where the mapping file(s) are.

这篇关于获取严重性:在JNDI中,无法找到SessionFactory,同时会话sessionfactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆