日期:2014-05-17  浏览次数:20803 次

学习hibernate,报错。请高手看看!
1.java文件
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.test.hibernate.model.Student;


public class StudentTest {

/**
* @param args
*/
public static void main(String[] args) {
Student student = new Student();
student.setId(1);
student.setName("test");
student.setAge(25);
 
Configuration cf = new Configuration();
SessionFactory sf = cf.configure().buildSessionFactory(); Session session = sf.openSession();
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
 
session.close();
sf.close();
 
}
}

2.Student.hbm.xml文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.pzq.hibernate.model">
  <class name="Student" table="Student">
  <id name="id" column="id"></id>
  <property name="name" column="name"/>
  <property name="age" column="age"/>
  </class>
</hibernate-mapping>

3.hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>

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

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

<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</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">update</property> -->


<mapping resource="com/test/hibernate/model/Student.hbm.xml"/>

</session-factory>
</hibernate-configuration>
4.异常见下
Exception in thread "main" java.lang.NoClassDefFoundError: javax/persistence/EntityListeners
at org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.getDefaults(JPAMetadataProvider.java:96)
at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.getDefaults(JavaRefle