错误如下:
Exception in thread “main” java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/MetadataProvider
at StudentTest.main(StudentTest.java:10)
Caused by: java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.MetadataProvider
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
… 1 more
StudentTest类的代码:
Exception in thread “main” java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/MetadataProvider
at StudentTest.main(StudentTest.java:10)
Caused by: java.lang.ClassNotFoundException: org.hibernate.annotations.common.reflection.MetadataProvider
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
… 1 more
StudentTest类的代码:
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.hibernate.Student; public class StudentTest { public static void main(String[] args) { Student s=new Student(); s.setName("sa"); s.setAge(12); Configuration cf=new Configuration(); SessionFactory sf=cf.configure().buildSessionFactory(); Session session=sf.openSession(); session.beginTransaction(); session.save(s); session.getTransaction().commit(); session.close(); sf.close(); } }
student.hbm.xml代码 (IE打开没有显示错误)
<?xml version="1.0" encoding="UTF-8"?> <!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.hibernate"> <class name="Student"> <id name="id"></id> <property name="name"></property> <property name="age"></property> </class> </hibernate-mapping>
hibernate.cfg.xml代码(IE打开没有显示错误)
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">12345678</property> <!-- <property name="hibernate.connection.pool.size">1</property> --> <property name="hibernate.show_sql">true </property> <!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> --> <property name="connection.characterEncoding">UTF-8</property> <!-- <property name="hbm2ddl.auto">update</property> --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property> <mapping resource="com/hibernate/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
student.java代码如下
package com.hibernate; public class Student { private int age; private String name; private int id; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
导入的hibernate的jar包如下
解决方案
5
缺这个包:
hibernate-commons-annotations
hibernate-commons-annotations
5
下边这个,是原因是版本不匹配吧
10
出现这种错误一般都是XML的格式有问题,如文件头有空行、有注释等
“[xX][mM][lL]” is not allowed.
“[xX][mM][lL]” is not allowed.
去掉空行或注释就可以正常启动。
“[xX][mM][lL]” is not allowed.
“[xX][mM][lL]” is not allowed.
去掉空行或注释就可以正常启动。
10
导入的包有问题,缺少一些包,hibernate3.jar,commons-collections.jar,加入这些包就可以