结构。我的cfg和hbm文件都在src的根目录下。在这里输入图片描述所有的罐子也包括在内。请在此输入图片描述但仍然得到这个问题.请帮助别人.我试图寻找解决方案,从过去的2天.谢谢。
Emp.java
public class Emp
{
private int id;
private String name,cmpname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCmpname() {
return cmpname;
}
public void setCmpname(String cmpname) {
this.cmpname = cmpname;
}
}
测试.java
package com.oehm.hibernatedemo;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Test {
public static void main(String[] args) {
Emp e = new Emp();
e.setId(1);
e.setName("harsh");
e.setCmpname("vardhan");
try
{
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
Session sess=factory.openSession();
sess.beginTransaction();
sess.save(e);
sess.getTransaction().commit();
}
catch (HibernateException exception)
{
exception.printStackTrace();
}
}
}
hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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.oehm.hibernatedemo.Emp" table="user01">
<id name="id"></id>
<property name="name"></property>
<property name="cmpname"></property>
</class>
</hibernate-mapping>
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">
<!-- Generated by MyEclipse Hibernate Tools -->
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hiber</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.show_sql">true</property>
<mapping class="emp.hbm.xml"/>
</session-factory>
</hibernate-configuration>
错误的属性 class
是用来代替 resource
以包含映射文件。应该是:
<mapping resource="emp.hbm.xml"/>