变得越来越严重:在获得sessionfactory时无法在JNDI中找到SessionFactory [英] Getting SEVERE: Could not locate SessionFactory in JNDI while getting sessionfactory
问题描述
我正在使用休眠作为持久层与数据库进行通信.我使用maven插件hbm2java生成dao,java,* hbm.xml和hibernate.cfg.xml.直到那时,插件才能正常工作.
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.
但是当我尝试与db通信时,会出现以下异常.
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)
默认情况下,hibernate3:hbm2cfgxml生成会话工厂名称为SessionFactory的hibernate.cfg.xml文件.就像跟随.
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");
}
}
我发现了类似的查询使用eclipse的问题Hibernate插件-无法在JNDI中找到sessionfactory .
但是,更改由插件生成的400多个类以从* Home.java获取会话工厂并不可行.
However it not feasible to change 400+ classes generated by plugin to get session factory from *Home.java.
如何在不更改400多个类的情况下使用hibernate插件解决此问题?
How to get rid of this problem with the hibernate plugin without changing 400+ classes?
我不想使用任何Web/应用程序服务器,这是我的独立应用程序.
I do not want to use any web/app server this is my standalone application.
推荐答案
只有在设置环境使InitialContext
可以找到hibernate.cfg.xml
文件时,使用生成的getSessionFactory()
方法才有效.
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.
您可以通过根据InitialContext
javadocs .作为替代方案,您可以通过更改getSessionFactory
方法以仅创建
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.
这篇关于变得越来越严重:在获得sessionfactory时无法在JNDI中找到SessionFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!