我从数据库加载对象,但出现以下异常,请帮助我,我厌倦了在我的休眠应用程序中找到错误,
例外
线程“主”org.hibernate.InvalidMappingException 中的异常:无法从 org.hibernate.cfg.Configuration.addResource(Configuration.java:569) 的 org.hibernate.cfg.Configuration 中的资源 Select.hbm.xml 解析映射文档。在 org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534) 在 org.hibernate.cfg.Configuration 的 org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555) 的 parseMappingElement(Configuration.java:1587) .doConfigure(Configuration.java:1508) at org.hibernate.cfg.Configuration.configure(Configuration.java:1428) at com.java4s.select.SelectMain.main(SelectMain.java:12) 原因:org.hibernate。 MappingException:在查找属性时未找到类 com.java4s.select.Select.java:org.hibernate.util.ReflectHelper 的 sno。在 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) at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:504) at org.hibernate.cfg.Configuration.addResource(Configuration.java:566) 原因:java.lang .ClassNotFoundException: com.java4s.select.Select.java at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1。在 sun.misc.Launcher$AppClassLoader 的 java.lang.ClassLoader.loadClass(Unknown Source) 在 java.net.URLClassLoader.findClass(Unknown Source) 在 java.security.AccessController.doPrivileged(Native Method) 运行(Unknown Source)。 loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.hibernate.util.ReflectHelper。 classForName(ReflectHelper.java:100) 在 org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:70) ... 14 更多ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100) at org .hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:70) ... 还有 14 个ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100) at org .hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:70) ... 还有 14 个
选择.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;
}
}
选择Main.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());
}
}
休眠.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>
选择.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>