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

来源:互联网 发布:阿里云 备案服务号 编辑:程序博客网 时间:2024/05/16 09:01

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所以会报这个错。

<hibernate-configuration>   <session-factory name="">      <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>        <property name="connection.url">jdbc:sqlserver:URL###</property>      <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>            <property name="connection.username">NAME</property>      <property name="connection.password">PASS</property>         <property name="hibernate.default_schema">dbo</property>            <mapping class="za.co.epsilon.domain.OverrideItemDo" />      <mapping class="za.co.epsilon.domain.BatchOverrideDo" />      <mapping class="za.co.epsilon.domain.TaskOverrideDo" />   </session-factory></hibernate-configuration>

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:

<session-factory>  不要再里面加属性<session-factory name=""> 否则会报错

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;    }