我使用hibernate作爲持久層與數據庫進行通信。我使用maven插件hbm2java生成dao,java,* hbm.xml和hibernate.cfg.xml。直到那時插件工作正常。獲取SEVERE:無法在JNDI中找到SessionFactory,而geeting sessionfactory
但是,當我試圖溝通數據庫它給出了以下例外。
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文件。就像下面一樣。
<session-factory name="SessionFactory">
插件生成下面的代碼獲取會話工廠。
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");
}
}
我發現了類似的查詢Problems using eclipse Hibernate plugin - could not locate sessionfactory in JNDI。
但是,改變插件生成的400多個類不可行,以便從* Home.java獲取會話工廠。
如何擺脫這個問題與休眠插件而不改變400 +類?
我不想使用任何Web /應用程序服務器這是我的獨立應用程序。
8月至今 –
這裏的31號沒有回答一個答案我張貼了類似的問題 http://stackoverflow.com/questions/16380304/java-lang-illegalstateexception-could-not-locate-sessionfactory -in-JNDI/16435578#16435578 – concept