我是hibernate框架的新手。我做了一个示例项目来测试hibernate。我建立了这个项目。但是当我运行它时我得到了这个错误
org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:972)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:69)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:966)
我放在项目主程序包中的 Hibernate.cfg.xml 文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.DerbyDialect
</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="connection.pool_size">2</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!--<property name="htm2ddl.auto">create</property>-->
<property name="hibernate.connection.driver_class">
org.apache.derby.jdbc.ClientDriver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:derby://localhost:1527/NetworkDB
</property>
<property name="hibernate.connection.username">
</property>
<property name="hibernate.connection.password">
</property>
<!-- List of XML mapping files -->
<!-- <mapping resource="Employee.hbm.xml"/>-->
</session-factory>
</hibernate-configuration>
我正在使用netbeans 7.0
答案 0 :(得分:1)
根据DTD,<hibernate-configuration>
应该有一个<session-factory>
,但您已经宣布了两次。
关于第二个错误:
org.hibernate.HibernateException: /hibernate.cfg.xml not found
Hibernate在类路径的根目录中查找配置文件,因此请检查是否已将此文件放在类路径的根目录下。
因此,一旦构建项目,hibernate.cfg.xml
文件应该在classes目录中。
答案 1 :(得分:1)
在xml的第一行添加以下内容:
<?xml version="1.0" encoding="UTF-8"?>