单独使用Hibernate配置注解报错An AnnotationConfiguration instance is required to use <mapping class=" ">

以下是在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 <mapping class=“com.entity.Emp”/>
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 <mapping class=“com.entity.Emp”/>,其翻译为注释配置实例需要使用的这个映射类无法扫描到。主要原因是当前的项目不支持注解。

解决方法:我是使用Myeclipse测试的,在项目名上右击,选择“Myeclipse”->Project facets->Install Hibernate facets。点击Next。

发现它默认就是支持注解。

输入对应的配置信息,点击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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值