根据HIBERNATE的文档,HIBERANTE是无法帮定到TOMCAT的JNDI的。
因此你只能用那个ThreadLoal的方法

解决方案 »

  1.   

    相关描述:
    If you wish to have the SessionFactory bound to a JNDI namespace, specify a name (eg. java:comp/env/hibernate/SessionFactory) using the property hibernate.session_factory_name. If this property is omitted, the SessionFactory will not be bound to JNDI. (This is especially useful in environments with a read-only JNDI default implementation, eg. Tomcat.) 
      

  2.   

    我靠,我也遇到了和楼主一样的问题。我在hibernate.cfg.xml里面配置了connection了,
    代码中也是openSession的时候报错了
    sessionFactory = new Configuration().configure().buildSessionFactory();
    ...
    session = sessionFactory.openSession();
    错误信息:
    信息: Not binding factory to JNDI, no JNDI name configured我就奇怪,没有用tomcat的jndi数据源,他怎么就去找jndi了呢?
      

  3.   

    sessionFactory = new Configuration().configure().buildSessionFactory();
    难道这个不是使用hibernate.cfg.xml中配置的数据源吗?
      

  4.   

    pbMaster(编程无境界) 有启发,但我还是不明白,specify a name (eg. java:comp/env/hibernate/SessionFactory) using the property hibernate.session_factory_name. 到底要怎样作(我有点死板(:)...).是照着它那样写呢还把“hibernate/SessionFactory”替换成什么东西。那好像不是hibernate.cfg.xml文件的写法。camel20(no silver bullet) 的问题也是我想问的,请知道 的同志帮助一下。
    session = sessionFactory.openSession();好像还有一个带参数的sessionFactory.openSession(conn);是不是可以给入一个数据连接,但现在的问题是我就根本到不了这一步,
    在“sessionFactory = cfg.buildSessionFactory();”时就出错了。
      

  5.   

    我不是说了吗,你不能将SessionFactory绑定到JNDI
    你只能用文档里面的那个自己写程序管理SessionFactory的例子。
      

  6.   

    楼主我的问题解决了.
    ...
    信息: Not binding factory to JNDI, no JNDI name configured
    ...
    这一句是正常的,注意“信息”不是错误,错误是其他的地方,这里根本就没有错误
      

  7.   

    我按照Hibernate Reference Documentation的 Chapter 1. Quickstart with Tomcat遇到如下问题
    java.lang.ExceptionInInitializerError
    at net.sf.hibernate.examples.quickstart.HibernateUtil.<clinit>(HibernateUtil.java:26)
    at net.sf.hibernate.examples.quickstart.TestHibernateUtil.setUp(TestHibernateUtil.java:12)
    ...(Click for full stack trace)...
    Caused by: net.sf.hibernate.HibernateException: Could not find datasource
    at net.sf.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:48)
    at net.sf.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:83)
    at net.sf.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:64)
    at net.sf.hibernate.cfg.Configuration.buildSettings(Configuration.java:1132)
    at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:766)
    at net.sf.hibernate.examples.quickstart.HibernateUtil.<clinit>(HibernateUtil.java:23)
    ... 11 more
    Caused by: 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:640)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)
    at javax.naming.InitialContext.lookup(InitialContext.java:347)
    at net.sf.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:44)
    ... 16 more
      

  8.   

    上面的问题是使用JUnit测试时遇到的,用servlet测试已经没有问题了。但是,对提示信息“no JNDI name configured”不理解,按照文档上做的不是设置了JNDI吗?
      

  9.   

    我是在“sessionFactory = cfg.buildSessionFactory();”
    的时候出错,走不到下一步threadLocal.set(session);
    页面显示:
    type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: Servlet execution threw an exception
    root cause java.lang.NoClassDefFoundError: javax/transaction/Synchronization
    net.sf.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:313)
    net.sf.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:317)
    net.sf.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:330)
    com.lis.framwork.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:63)
    com.lipile.struts.action.LoginAction.execute(LoginAction.java:59)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      

  10.   

    时提示:“Not binding factory to JNDI, no JNDI name configured”。的原因找到了,但问题还是没有解决,这个提示是在XML的配置中缺一个name,这里:<session-factory>加name="XXX"就不会出现这个提示了,见下面,但出现新的问题:
    信息: building session factory
    2004-11-26 15:41:50 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
    信息: Factory name: foo
    2004-11-26 15:41:50 net.sf.hibernate.util.NamingHelper getInitialContext
    信息: JNDI InitialContext properties:{}
    2004-11-26 15:41:50 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
    信息: Bound factory to JNDI name: foo
    2004-11-26 15:41:50 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
    警告: InitialContext did not implement EventContext
    这里是什么问题。