无法从资源Select.hbm.xml解析映射文档

时间:2013-08-22 12:49:13

标签: hibernate hibernate-mapping

我从数据库加载对象,但我得到以下异常,请帮助我,我厌倦了在我的休眠应用程序中找到错误,

异常

  

线程中的异常" main" org.hibernate.InvalidMappingException:   无法从资源Select.hbm.xml解析映射文档       在org.hibernate.cfg.Configuration.addResource(Configuration.java:569)       在org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)       在org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)       在org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)       在org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)       在org.hibernate.cfg.Configuration.configure(Configuration.java:1428)       在com.java4s.select.SelectMain.main(SelectMain.java:12)       引起:org.hibernate.MappingException:       找到属性时找不到类com.java4s.select.Select.java:sno       在org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:74)       在org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:276)       在org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:401)       在org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:334)       在org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:273)       在org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:144)       在org.hibernate.cfg.Configuration.add(Configuration.java:669)       在org.hibernate.cfg.Configuration.addInputStream(Configuration.java:504)       在org.hibernate.cfg.Configuration.addResource(Configuration.java:566)       引起:java.lang.ClassNotFoundException:com.java4s.select.Select.java       在java.net.URLClassLoader $ 1.run(未知来源)       at java.net.URLClassLoader $ 1.run(Unknown Source)at java.security.AccessController.doPrivileged(Native Method)at at   java.net.URLClassLoader.findClass(未知来源)at   java.lang.ClassLoader.loadClass(未知来源)at   sun.misc.Launcher $ AppClassLoader.loadClass(未知来源)at   java.lang.ClassLoader.loadClass(未知来源)at   java.lang.Class.forName0(Native Method)at   java.lang.Class.forName(未知来源)at   org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)   在   org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:70)       ......还有14个

Select.java

public class Select {
private int sno;
private String uname;
private String pass;

public int getSno() {
    return sno;
}
public void setSno(int sno) {
    this.sno = sno;
}
public String getUname() {
    return uname;
}
public void setUname(String pass) {
    this.pass =pass;
}
public String getPass() {
    return pass;
}
public void setPass(String pass) {
    this.pass =pass;
}


}

SelectMain.java

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

public class SelectMain {

public static void main(String[] args) {

Configuration cfg=new Configuration();
cfg.configure("Hibernate.cfg.xml");
SessionFactory factory=cfg.buildSessionFactory();
Session session=factory.openSession();
Object o=session.load(Select.class,new Integer(1));
Select s=(Select) o;
System.out.println(s.getUname());
}
}

hibernate.cfg.xml中

<?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>    
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/world</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>    
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2dll.auto">update</property>    
<mapping resource="Select.hbm.xml"></mapping>
</session-factory>    
</hibernate-configuration>

Select.hbm.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>
<class name="com.java4s.select.Select.java" table="product1">   
<id name="sno" column="sno"/>    
<property name="uname" column="uname"/>    
<property name="pass" column="pass"/>    
</class>
</hibernate-mapping>

3 个答案:

答案 0 :(得分:1)

你应该使用(包的)完整路径来编写hibernate.cfg.xml中的资源:

<mapping resource="com/java4s/select/Select.hbm.xml"></mapping>

同时检查hbm文件是否与java类文件位于同一个包中。

编辑: 您对sno属性的映射存在问题。检查hibernate mapping documentation。 我猜您应该为id字段分配一个生成器,例如:

 <id column="sno" name="sno">
      <generator class="assigned"/>
 </id>

答案 1 :(得分:0)

在Select.hbm.xml文件中 class name =“com.java4s.select.Select.java”table =“product1” 如下所示更改此行 class name =“com.java4s.select.Select”table =“product1”

您应该只提供班级名称。 .java不是必需的。

答案 2 :(得分:0)

在Select.hbm.xml文件中更改类名 <class name="com.java4s.select.Select.java" table="product1"><class name="com.java4s.select.Select" table="product1">