为了账号安全,请及时绑定邮箱和手机立即绑定

单元测试通过不了

单元测试通过不了,第一次搞hibernate,也不知道日志里边测错误提示在哪。请大神帮忙看看。谢谢


org.hibernate.MappingException: Unknown entity: userHibernate.Students

at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)

at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1634)

at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)

at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)

at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)

at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)

at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)

at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)

at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:689)

at org.hibernate.internal.SessionImpl.save(SessionImpl.java:681)

at org.hibernate.internal.SessionImpl.save(SessionImpl.java:676)

at userHibernate.StudentsTest.testSaveStudents(StudentsTest.java:49)

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:498)

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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)

at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)

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:678)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


这是我的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 name="">
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;amp;characterEncoding=UTF-8</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">dasfsa</property>
  <property name="show_sql">true</property>
  <property name="format_sql">true</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hbm2ddl.auto">create</property>
  <mapping resource="Students.hbm.xml"/>
 </session-factory
</hibernate-configuration>


正在回答

2 回答

看些我总结的 里面有http://www.cnblogs.com/jpwahaha/p/6834102.html

2 回复 有任何疑惑可以回复我~
#1

Ziloon_Z 提问者

原因果然是新的hibernate库的创建服务注册方法不一样。应该是用: ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()) .configure().build();
2017-05-16 回复 有任何疑惑可以回复我~
#2

老季

能对你有帮助,真的很高心
2017-05-19 回复 有任何疑惑可以回复我~
#3

小蜗牛有大理想

感谢题主,把解决方案贴出来,我犯得是第二个错误。上网找了两个小时答案,终于在你这找到了,拜谢!!!
2017-12-28 回复 有任何疑惑可以回复我~

非常感谢题主找到答案后,将解决方案贴出来,解决了后来者相同处的困惑。Thanks!

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

单元测试通过不了

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信