单独使用Hibernate配置注解报错An AnnotationConfiguration instance is required to use

以下是在Junit上测试是报错的全部内容:
java.lang.ExceptionInInitializerError
at com.test.ManyToOne.test1(ManyToOne.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.hibernate.MappingException: An AnnotationConfiguration instance is required to use
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1648)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1603)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1582)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1556)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
at com.util.HibernateUtil.(HibernateUtil.java:18)
… 24 more

主要报错内容:An AnnotationConfiguration instance is required to use ,其翻译为注释配置实例需要使用的这个映射类无法扫描到。主要原因是当前的项目不支持注解。

解决方法:我是使用Myeclipse测试的,在项目名上右击,选择“Myeclipse”->Project facets->Install Hibernate facets。点击Next。
单独使用Hibernate配置注解报错An AnnotationConfiguration instance is required to use_第1张图片
发现它默认就是支持注解。
单独使用Hibernate配置注解报错An AnnotationConfiguration instance is required to use_第2张图片
输入对应的配置信息,点击finish即可。会自动导入支持Hibernate包括注解所需的一系列jar包,如ejb3-persistance.jar、hibernate-commons-annotations.jar、hibernate-annotations.jar等。

如国是使用Spring整合的Hibernate配置,那么只需要在spring 的配置文件中的sessionFactory标签的子标签class内将
org.springframework.orm.hibernate3.LocalSessionFactoryBean
改成
org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean即可。
注意引入Hibernate的配置文件hibernate.cfg.xml。

你可能感兴趣的:(hibernate整合)