使用AnnotationConfiguration(hibernate)连接到数据库时出错

时间:2016-05-13 08:08:11

标签: hibernate annotations

/*
This is the TableCreater class used for creating the database tables . I am getting an error at the line with :cfg = new AnnotationConfiguration(). 

*/

package DAOLayer;
import OEMEntities.*;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/*
Creates the tables for OEW database
*/

public class TableCreater
{

public static void main(String[] args)
{
AnnotationConfiguration cfg;
cfg = new AnnotationConfiguration();
cfg.addAnnotatedClass(Disk_Master.class);
cfg.addAnnotatedClass(Order_Master.class);
cfg.addAnnotatedClass(Stock_Master.class);
cfg.addAnnotatedClass(Member_Detail.class);
cfg.addAnnotatedClass(User_Account.class);
cfg.addAnnotatedClass(Login_Master.class);
cfg.configure();//gets info from database
SchemaExport se = new SchemaExport(cfg);
se.create(true, true);//1. to implement query building and display in log,2. execute build query
System.out.println("TABLE CREATED");
}
}

运行:

Exception in thread "main" java.lang.IncompatibleClassChangeError: Implementing class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)  at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at DAOLayer.TableCreater.main(TableCreater.java:22)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

This is my hibernate.cfg.xml

1 个答案:

答案 0 :(得分:0)

版本3.6中不推荐使用

AnnotationConfiguration。您必须使用Configuration来解决此问题,如下所示。

Configuration cfg;
cfg = new Configuration();