首先,谢谢你这么详细的回答! 但是,我又试了一下,发现新的问题,我把那个hibernate的配置文件如果换成hibernate.properties的话,就不会出现错误提示,只是说: 09:45:12,875 WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/hibernate-3.0/lib/ehcache-1.1.jar!/ehcache-failsafe.xml 09:45:13,437 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured 然而,换成hibernate.cfg.xml还是出现上面同样的错误提示,我做的程序没有在tomcat环境下,只是一个application程序而已!我又下了一个hibernate3.0的包,把相应的文件和包换掉,仍然出现上面相同的问题!我想应该是JNDI初始化的问题,那为什么用hibernate.properties就不出错呢?不理解??请指教!! 还有,在Application或是普通JavaBean中如何初始化JNDI呢?
JNDI没有被正确初始化?请详细指点!
SessionFactory在通过配置文件来初始化的时候,如果设置了DataSource,就会查找当前jvm中的JNDI Context,把数据源绑定到Context中,如果jvm不存在一个被初始化的JNDI Context,就会出现你这个错误如果程序是在tomcat等环境下运行的,由于tomcat会初始化JNDI,所以不会有问题当然,JNDI也可以脱离tomcat等环境,在Application或是普通JavaBean中被初始化
可参考:http://www.webjx.com/htmldata/2005-09-23/1127431793.html
但是,我又试了一下,发现新的问题,我把那个hibernate的配置文件如果换成hibernate.properties的话,就不会出现错误提示,只是说:
09:45:12,875 WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/hibernate-3.0/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
09:45:13,437 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
然而,换成hibernate.cfg.xml还是出现上面同样的错误提示,我做的程序没有在tomcat环境下,只是一个application程序而已!我又下了一个hibernate3.0的包,把相应的文件和包换掉,仍然出现上面相同的问题!我想应该是JNDI初始化的问题,那为什么用hibernate.properties就不出错呢?不理解??请指教!!
还有,在Application或是普通JavaBean中如何初始化JNDI呢?
>>>请把这两种配置文件都帖出来看看
还有,在Application或是普通JavaBean中如何初始化JNDI呢?>>>我前面已经给你一个链接地址了,请自己去查阅实现方法
>>>http://www.webjx.com/htmldata/2005-09-23/1127431793.html
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"><hibernate-configuration>
<session-factory name="hibernate_connection_factory"> <!-- local connection properties -->
<property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.0.2:1521:dhbdb</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.username">lott</property>
<property name="hibernate.connection.password">lott</property>
<!-- property name="hibernate.connection.pool_size"></property -->
<!-- dialect for Oracle 9 -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="net/sf/hibernate/mapinfo/EpEmployee.hbm.xml" />
</session-factory>
</hibernate-configuration>hibernate.properties
hibernate.dialect net.sf.hibernate.dialect.Oracle9Dialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username lott
hibernate.connection.password lott
hibernate.connection.url jdbc:oracle:thin:@localhost:1521:dhbdb
中的name="hibernate_connection_factory"去掉。指定name后,hibernate会试着以指定的name去将SessionFactory的一个实例绑定在jndi下(这样通过javax.naming.Context.lookup(String name)的方式就可以找到),但在
application或像tomcat这样不支持jndi动态绑定的环境下,当然会出现问题。