错误信息:
Name hibernate is not bound in this ContextContext导入的是javax.naming.Context;
在网上查了资料,说是没有绑定JNDI,可我就是按孙卫琴的<精通hibernate>一步一步来的,而且也在网上查了大部分资料,大体就是这个步骤,为什么呢?如果tomcat不配置数据源,用hibernate自己的链接机制,也可以正常运行
如果配置tomcat数据源,而不用hibernate技术,也没问题
只要tomcat数据源与hiberate连用就了这问题,天哪我该怎么办?
有人说如果要用tomcat数据源,hibernate必须搭配spring技术取得数据源
我晕,我想hibernate不至于这么差吧?

解决方案 »

  1.   

    在Webapps的Web.xml中改为以下内容:
    <resource-ref>
    <description>connectDB test</description>
    <res-ref-name>jdbc/hibernate</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>
      

  2.   

    jndi服务名是java:/hibernate/HibernateFactory的sessionfactory是不是也要在web.xml中项数据源一样注册声明一下
      

  3.   

    郁闷!
    难道就没有人用过hibernate?都是与spring联合使用??????????????????
      

  4.   

    如果用中间件的连接池是这样的:1、首先在中间件上建立数据源。2、在这个数据源的基础上建立一个jndi(tomcate不太懂,但如果是weblogic,在控制台就可以建立)3、修改hibernate.cfg.xml文件4、修改web.xml文件5、应用。
      

  5.   

    Context ctx = new InitialContext();
    String jndiName = "java:/hibernate/HibernateFactory";
    sf = (SessionFactory)ctx.lookup(jndiName);
    session = sf.openSession();Hiberbate里面session不是这样取的,Session s=new Configuration().configure("hibernate.cfg.xml 为止").buildSessionFactory().openSession();
    hibernate里面不用象上面这么麻烦
      

  6.   

    我用struts和hibernate做过一个例子,也是绑定JNDI的,不过不像你这样在配置文件中配置,所以也不知道你这样配置是否正确,我把我做的给你看看吧,也许有点帮助。
    首先是初始化类,通过读取配置文件实例化hibernate.cfg.xml文件,并绑定到JNDI中ctx.bind("HibernateSessionFactory", sessionFactory);:public class HibernateInit implements PlugIn{   private Context ctx;
       private SessionFactory sessionFactory;
       private String hbnConfigFile;   public void init(ActionServlet servlet, ModuleConfig config) throws ServletException{
           try{
              Configuration cfg = new Configuration();
              cfg.configure(this.getHbnConfigFile());
              sessionFactory = cfg.buildSessionFactory();
           }catch(HibernateException ex){
              throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
           }
            
           try{
              ctx = new InitialContext();
              ctx.bind("HibernateSessionFactory", sessionFactory);
           }catch(NamingException ex){
              throw new RuntimeException("Exception building SessionFactory22: " + ex.getMessage(), ex);
           }
       }   public void destroy(){
           if(ctx!=null){
              try{
                 ctx.unbind("HibernateSessionFactory");
              }catch(NamingException e){
                 e.printStackTrace();
              }
           }
           if(sessionFactory!=null){
              try{
                 sessionFactory.close();
              }catch(HibernateException e){
                  e.printStackTrace();
              }
              sessionFactory = null;
           }
       }   public void setHbnConfigFile(String hbnConfigFile){
          this.hbnConfigFile = hbnConfigFile;
       }
       public String getHbnConfigFile(){
          return hbnConfigFile;
       }}接着就在工具类中使用该JNDI。public class DButil{   private static SessionFactory sessionFactory = null;
       public static final ThreadLocal session = new ThreadLocal();   public static Session currentSession() throws HibernateException{
          if(sessionFactory == null){
              if(getSystemSessionFactory() == false){
                  throw new HibernateException("Exception geting SessionFactory from JNDI");
              }
          }      Session s = (Session)session.get();
          if(s == null){
             s = sessionFactory.openSession();
             session.set(s);
          }      return s;
       }   public static void closeSession() throws HibernateException{
           Session s = (Session)session.get();
           session.set(null);
           if(s!=null){
               s.close();
           }
       }   public static boolean getSystemSessionFactory(){
           try{
              Context ctx = new InitialContext();
              sessionFactory = (SessionFactory)ctx.lookup("HibernateSessionFactory");
           }catch(NamingException e){
              return false;
           }
           return true;
       }}
    这里就通过语句sessionFactory = (SessionFactory)ctx.lookup("HibernateSessionFactory");获得sessionfactory。
      

  7.   

    最后在business中就可以那么取得session了:
    Session session = DButil.currentSession();
          Transaction tx = session.beginTransaction();      Criteria criteria  = session.createCriteria(User.class);
          criteria.add(Expression.like("name", userName));
          List result = criteria.list();      tx.commit();
          DButil.closeSession();
      

  8.   

    to:zhangj0571(笨鸟飞飞)
    还是不行,报这个错误:Could not find datasource
      

  9.   

    to:wangshenhai() 
    User.class
    可我的配制文件为.xml格式的
      

  10.   

    String jndiName = "java:/hibernate/HibernateFactory";应该是String jndiName = "jdbc/UserDB";吧。
      

  11.   

    你一下子搞出这么多环节,很难搞清楚是哪个环节出了问题,你应该逐个环节试一下1.tomcat配置了数据源
    2.web.xml
    这个你可以在jsp页面里试一下
    Context ctx = new InitialContext();
    String jndiName = "java:comp/env/jdbc/UserDB";
    DataSource ds = (DataSource)ctx.lookup(jndiName);
    Connection conn = ds.getConnection();
    ResultSet rs = conn.executeQuery("select sysdate()");
    if(rs.next()){
      out.println(rs.getString(1));
    }这一步通过了再试下一步
      

  12.   

    这个没问题
    我在jsp页调试成功
    不用hibernate调试成功
    用hibernate自带链接池机制也调试成功
    就是用tomcat链接池时,再用hibernate读tomcat数据源时就报错
    框架struts+hibernate
      

  13.   

    3,4改成如下呢:
    3.hibernate.cfg.xml
    <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="connection.datasource">java:comp/env/jdbc/UserDB</property>
    <property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
    <property name="transaction.manager_lookup_class">org.hibernate.transaction.JbossTransactionManageLookup</property>
    <property name="show_sql">true</property><mapping resource="user/UserBean.hbm.xml"/></session-factory>4.bean:
    SessionFactory sf = new Configuration().configure().buildSessionFactory();
    session = sf.openSession();试一下
      

  14.   

    楼主,应该不是hibernate的问题,老孙那书我也看了
    试着调试好像不对
    你用tomcat admin 那个图形化的东东里面配置一下datasource,看看和你手动写的配置
    放置的位置是不是一样就知道了
      

  15.   

    像dreamover(梦醒了)说的那样,还是一步一步调试比较好吧
      

  16.   

    数据源配置没有问题
    如果不用hibernate可以取到数据源,且能正常运行