i loading the object from the database,but i got the following exception,please some help me i tired to find the error in my hibernate application,
Exception
Exception in thread "main" org.hibernate.InvalidMappingException:
Could not parse mapping document from resource Select.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at com.java4s.select.SelectMain.main(SelectMain.java:12)
Caused by: org.hibernate.MappingException:
class com.java4s.select.Select.java not found while looking for property: sno
at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:74)
at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:276)
at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:401)
at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:334)
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:273)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:144)
at 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)
Caused by: java.lang.ClassNotFoundException: com.java4s.select.Select.java
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at
java.net.URLClassLoader.findClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
sun.misc.Launcher$AppClassLoader.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)
at
org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:70)
… 14 more
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>
Best Answer
You should mape the resource in the
hibernate.cfg.xml
with the full path (of the package):Also check that the
hbm
file is in the same package as thejava
class file.EDIT: You have a problem with the mapping of the
sno
property. Check that hibernate mapping documentation. I guess you should assign a generator to yourid
field, like for example: