2012-01-31 116 views
1

對於您所有的信息,我正在開發一個使用Eclipse與Eclipse進行交互的java項目,因此我使用ojdbc14.jar和hibernate3.jar以及所有7個jar文件更新了BUILD PATH Hibernate所需的文件夾。與Hibernate進行交互時出錯

我在這裏粘貼了所有課程。 在運行主Manager.java,我得到一個錯誤「異常線程‘main’org.hibernate.InvalidMappingException:無法從資源解析映射文件COM /拉拉/ Person.hbm.xml」。可有人告訴哪裏我犯了一個錯誤。

錯誤日誌

Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/lara/Person.hbm.xml 
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:616) 
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1635) 
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1603) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1582) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1556) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1476) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1462) 
    at com.lara.Manager.main(Manager.java:16) 
Caused by: org.hibernate.MappingException: class org.hibernate.tutorial.domain.Person not found while looking for property: id 
    at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:232) 
    at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:302) 
    at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:423) 
    at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:356) 
    at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:295) 
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:166) 
    at org.hibernate.cfg.Configuration.add(Configuration.java:716) 
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:551) 
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:613) 
    ... 7 more 
Caused by: java.lang.ClassNotFoundException: org.hibernate.tutorial.domain.Person 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:303) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248) 
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316) 
    at java.lang.Class.forName0(Native Method) 
    at java.lang.Class.forName(Class.java:169) 
    at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:192) 
    at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:228) 
    ... 15 more 









**hibernate.cfg** 
     <?xml version='1.0' encoding='utf-8'?> 
    <!DOCTYPE hibernate-configuration PUBLIC 
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

    <hibernate-configuration> 

     <session-factory> 

     <!-- Database connection settings --> 
     <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
     <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property> 
     <property name="connection.username">System</property> 
     <property name="connection.password">java</property> 

     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">2</property> 

     <!-- SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.OracleDialect</property> 

     <!-- Enable Hibernate's current session context --> 
     <property name="current_session_context_class">org.hibernate.context.ManagedSessionContext</property> 

     <!-- Disable the second-level cache --> 
     <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 

     <!-- Echo all executed SQL to stdout --> 
     <property name="show_sql">true</property> 

     <!-- Drop and re-create the database schema on startup --> 
     <property name="hbm2ddl.auto">create</property> 

     <mapping resource="com/lara/Person.hbm.xml"/> 

    </session-factory> 

</hibernate-configuration> 

Person.bhm.xml

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

<hibernate-mapping package="org.hibernate.tutorial.domain"> 

    <class name="Person" table="PERSON"> 
     <id name="id" column="PERSON_ID"> 
      <generator class="native"/> 
     </id> 
     <property name="age"/> 
     <property name="firstname"/> 
     <property name="lastname"/> 

    </class> 

</hibernate-mapping> 

POJO類或持久性類

package com.lara; 

public class Person 
{ 
    private int id; 
    private String firstname; 
    private String lastname; 
    private int age; 
    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; 
    } 
    public int getAge() 
    { 
     return age; 
    } 
    public void setAge(int age) 
    { 
     this.age = age; 
    } 

} 

* Manager.java(這裏我們使用Hibernate框架主類)*

package com.lara; 

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

public class Manager 
{ 
    public static void main(String[] args) 
    { 
     Person p1 = new Person(); 
     p1.setFirstname("saurabh"); 
     p1.setLastname("rai"); 
     p1.setAge(25); 
     Configuration c1 = new Configuration(); 
     c1.configure(); 
     SessionFactory sf = c1.buildSessionFactory(); 
     Session s1 = sf.openSession(); 
     s1.beginTransaction(); 
     s1.save(p1); 
     s1.getTransaction().commit(); 
     s1.flush(); 
     s1.close(); 
     System.out.println("done"); 
    } 
} 
+0

@skaffman我只是忘了,因爲我在如癡如醉的狀態 – 2012-01-31 23:13:08

+0

仍不足以提的錯誤。除此之外,還有更多的錯誤。 – skaffman 2012-01-31 23:14:29

+0

@skaffman現在我粘貼了整個錯誤日誌,以前我遇到過這個問題 – 2012-01-31 23:18:14

回答

2

<hibernate-mapping package="org.hibernate.tutorial.domain">com.lara。你如何看待這兩者之間的關係?你的映射文件有一個錯誤的包聲明,Hibernate無法找到一個類org.hibernate.tutorial.domain.Person

1

嘗試在hbm.xml文件中更改你的hibernate-mapping中的包,使它與你的Person對象的包相匹配。你已經org.hibernate.tutorial.domain與com.lara

+0

查詢時我修改裏面的人映射 .hbm.xml然後運行正常。感謝您的支持。 – 2012-01-31 23:43:16