javax.naming.NoInitialContextException: Need to specify class name in environment or system property

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

在使用hibernate 3.5.1的时候   报错,原因是 hibernate 配置文件中给sessionFactory 配置了name属性,把这个属性删掉,否则会去找jndi,但是运行环境中没有使用jndi所以会报这个错。


   
      com.microsoft.sqlserver.jdbc.SQLServerDriver
        jdbc:sqlserver:URL###
      org.hibernate.dialect.SQLServerDialect
      
      NAME
      PASS
   
      dbo
      
      
      
      

   

You need to remove the name attribute from your opening session factory tag in your hibernate configuration file so that it looks like the following:

  不要再里面加属性 否则会报错

public static SessionFactory getSessionFactory() {
        String sfName = configuration.getProperty(Environment.SESSION_FACTORY_NAME);
        if ( sfName != null && !sfName.equals("")) {
            log.debug("Looking up SessionFactory in JNDI");
            try {
                return (SessionFactory) new InitialContext().lookup(sfName);
            } catch (NamingException ex) {
                throw new RuntimeException(ex);
            }
        } else if (sessionFactory == null) {
                 sessionFactory = new Configuration().configure().buildSessionFactory();
        }
        return sessionFactory;
    } 



你可能感兴趣的:(hibernate)