您的位置:首页 > 编程语言 > Java开发

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

2011-12-07 17:54 513 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐