Hibernate.config.xml文件错误

时间:2015-07-07 06:11:09

标签: java xml eclipse hibernate

我对Hibernate很新,http://www.javatpoint.com/example-to-create-hibernate-application-in-eclipse-ide
 我按照这个链接创建了一个Hibernate应用程序,但是我得到以下错误: 11:31:31,707 INFO [main]环境:584 - 使用JDK 1.4 java.sql.Timestamp处理 11:31:31,738 INFO [main]配置:1423 - 从资源配置:hibernate.cfg.xml 11:31:31,738 INFO [main]配置:1400 - 配置资源:hibernate.cfg.xml

11:31:33,501 INFO [main]配置:553 - 从资源中读取映射:employee.hbm.xml 线程" main"中的例外情况org.hibernate.MappingNotFoundException:resource:找不到employee.hbm.xml     在org.hibernate.cfg.Configuration.addResource(Configuration.java:563)     在org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1584)     在org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1552)     在org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1531)     在org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1505)     在org.hibernate.cfg.Configuration.configure(Configuration.java:1425)     at pack.storedata.main(storedata.java:13)

hibernate.cfg.xml中:

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration>
   <session-factory>

   <property name="hibernate.dialect">
      org.hibernate.dialect.MySQLDialect
   </property>
   <property name="hibernate.connection.driver_class">
      com.mysql.jdbc.Driver
   </property>

   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
      jdbc:mysql://localhost:3306/test
   </property>
   <property name="hibernate.connection.username">
    root
   </property>
   <property name="hibernate.connection.password">
      root
   </property>
   <!-- List of XML mapping files -->
   <mapping resource="employee.hbm.xml" />
</session-factory>
</hibernate-configuration> 

employee.hbm.xml:

<?xml version='1.0'?>  
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-mapping>
  <class name="pack.employee" table="emp1000">  
    <id name="id">  
     <generator class="assigned"></generator>  
    </id>  

    <property name="firstName"></property>  
    <property name="lastName"></property>  

  </class>  

 </hibernate-mapping>

storedata.java:

package pack;

import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.Transaction;  
import org.hibernate.cfg.Configuration;  

public class storedata {  
public static void main(String[] args) {  

    //creating configuration object  
    Configuration cfg=new Configuration();  
    cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file  

    //creating seession factory object  
    SessionFactory factory=cfg.buildSessionFactory();  

    //creating session object  
    Session session=factory.openSession();  

    //creating transaction object  
    Transaction t=session.beginTransaction();  

    employee e1=new employee();  
    e1.setId(115);  
    e1.setFirstName("sonoo");  
    e1.setLastName("jaiswal");  

    session.persist(e1);//persisting the object  

    t.commit();//transaction is committed  
    session.close();  

    System.out.println("successfully saved");  

}  
} 

employee.java:

package pack;


public class employee {  
private int id;  
private String firstName,lastName;  

public int getId() {  
  return id;  
}  
public void setId(int id) {  
  this.id = id;  
}  
public String getFirstName() {  
  return firstName;  
}  
public void setFirstName(String firstName) {  
  this.firstName = firstName;  
}  
public String getLastName() {  
  return lastName;  
}  
public void setLastName(String lastName) {  
  this.lastName = lastName;  
}  


}  

项目结构:

Hibernate
 ->src
   ->pack
     ->employee.java
     ->storedata.java
   -> employee.hbm.xml
   -> hibernate.cfg.xml

4 个答案:

答案 0 :(得分:2)

我认为您需要将hibernate.cfg.xml和employee.hbm.xml文件放在资源文件夹中。

<mapping resource="employee.hbm.xml/>

否则将hibernate.cfg.xml放在资源文件夹中,并将employee.hbm.xml与employee.java(model)放在一个单独的包中,然后在映射时需要提供正确的路径名:

<mapping resource="pack.employee.hbm.xml"/>

答案 1 :(得分:1)

只需进行以下更改即可 位于<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <ul class="list"> <li class="list-item" data-text="1 List Item">1</li> <li class="list-item" data-text="2 List Item">2</li> <li class="list-item" data-text="3 List Item">3</li> <li class="list-item" data-text="4 List Item">4</li> </ul> <p class="text-here"></p> -

> ActiveRecord::Base.connection.reset_pk_sequence!('table_name')

并且在 StoreData.java 文件

hibernate.cfg.xml

答案 2 :(得分:0)

错误信息非常清楚。

MappingNotFoundException: resource: employee.hbm.xml not found at 

我认为您错过了xml文件的映射,请尝试检查所需的xml文件,可能是您拼错了。

示例在我的机器中正常工作

答案 3 :(得分:0)

您缺少开始标记

<hibernate-mapping> 
您的employee.hbm.xml文件中的