org.hibernate.MappingException:找不到实体类(使用限定的类名不起作用)
Posted
技术标签:
【中文标题】org.hibernate.MappingException:找不到实体类(使用限定的类名不起作用)【英文标题】:org.hibernate.MappingException: entity class not found ( using qualified class name does not work) 【发布时间】:2012-06-27 21:12:00 【问题描述】:我正在尝试运行一个简单的 SELECT FROM 表(我也尝试过 SELECT FROM package.Class)查询,但我不断收到此错误:
org.hibernate.MappingException: entity class not found: demo.Agents
at org.hibernate.mapping.PersistentClass.getMappedClass(PersistentClass.java:125)
at org.hibernate.tuple.PropertyFactory.getGetter(PropertyFactory.java:191)
at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:67)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:136)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:475)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:133)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:297)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
at org.hibernate.console.ConsoleConfiguration$5.execute(ConsoleConfiguration.java:278)
at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:63)
at org.hibernate.console.ConsoleConfiguration.execute(ConsoleConfiguration.java:107)
at org.hibernate.console.ConsoleConfiguration.buildSessionFactory(ConsoleConfiguration.java:273)
at org.hibernate.eclipse.console.actions.ExecuteQueryAction.execute(ExecuteQueryAction.java:82)
at org.hibernate.eclipse.console.actions.ExecuteQueryAction.run(ExecuteQueryAction.java:56)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
at org.hibernate.eclipse.console.actions.ExecuteQueryAction.runWithEvent(ExecuteQueryAction.java:60)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
at org.eclipse.jface.action.ActionContributionItem$6.handleEvent(ActionContributionItem.java:452)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2701)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2665)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2499)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:679)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
Caused by: java.lang.ClassNotFoundException: demo.Agents
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:513)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.mapping.PersistentClass.getMappedClass(PersistentClass.java:122)
... 43 more
这里是类的 hbm.xml 文件(注意完整的限定符 demo.Agents)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 27, 2012 19:17:46 by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
<class name="demo.Agents" table="agents" schema="public">
<id name="id" type="int">
<column name="id" />
<generator class="assigned"></generator>
</id>
<property name="ssn" type="long">
<column name="ssn" precision="13" scale="0" not-null="true" />
</property>
<property name="namee" type="string">
<column name="namee" length="15" not-null="true" />
</property>
<property name="surname" type="string">
<column name="surname" length="30" not-null="true" />
</property>
<property name="adress" type="string">
<column name="adress" length="100" />
</property>
<property name="phone" type="string">
<column name="phone" length="20" />
</property>
</class>
</hibernate-mapping>
其他一些细节,可能有帮助:
http://img441.imageshack.us/img441/2572/ss20120627202116.png
http://img20.imageshack.us/img20/2236/ss20120624201620.png
如果您需要任何其他打印屏幕,我很乐意提供。请帮忙 :)。我处于一个非常紧张的位置。谢谢(有人添加了一些更好的标签,我现在压力很大,想太多了)
乐
hibernatecfg.xml
<?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">org.postgresql.Driver</property>
<property name="hibernate.connection.password">blablabla</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/projteste</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
</session-factory>
</hibernate-configuration>
映射在控制台配置中完成
LE2 查看 cmets(链接 607)如果我从控制台配置中的其他映射中删除类,则会收到该错误。
这里是 hibernatecfg.xml
<?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 name="">
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/projteste</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<mapping resource="demo/Clients.hbm.xml"/>
<mapping resource="demo/Agents.hbm.xml"/>
</session-factory>
</hibernate-configuration>
和堆栈跟踪
org.hibernate.MappingNotFoundException: resource: demo/Clients.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:665)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1679)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1647)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1626)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1600)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1554)
at org.hibernate.console.ConfigurationFactory.loadConfigurationXML(ConfigurationFactory.java:351)
at org.hibernate.console.ConfigurationFactory.configureStandardConfiguration(ConfigurationFactory.java:279)
at org.hibernate.console.ConfigurationFactory.buildConfiguration(ConfigurationFactory.java:184)
at org.hibernate.console.ConfigurationFactory.createConfiguration(ConfigurationFactory.java:96)
at org.hibernate.console.ConsoleConfiguration$3.execute(ConsoleConfiguration.java:240)
at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:63)
at org.hibernate.console.ConsoleConfiguration.execute(ConsoleConfiguration.java:107)
at org.hibernate.console.ConsoleConfiguration.buildWith(ConsoleConfiguration.java:237)
at org.hibernate.console.ConsoleConfiguration.build(ConsoleConfiguration.java:188)
at org.hibernate.eclipse.console.workbench.ConsoleConfigurationWorkbenchAdapter.getChildren(ConsoleConfigurationWorkbenchAdapter.java:42)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.getChildren(BasicWorkbenchAdapter.java:100)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:106)
at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:235)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
k4
org.hibernate.console.HibernateConsoleRuntimeException: Could not compute classpath
at org.hibernate.eclipse.console.EclipseLaunchConsoleConfigurationPreferences.getCustomClassPathURLS(EclipseLaunchConsoleConfigurationPreferences.java:103)
at org.hibernate.eclipse.console.utils.ProjectUtils.findJavaProjects(ProjectUtils.java:302)
at org.hibernate.eclipse.console.workbench.ProjectCompilerVersionChecker.validateProjectComplianceLevel(ProjectCompilerVersionChecker.java:35)
at org.hibernate.eclipse.console.workbench.ConsoleConfigurationWorkbenchAdapter.getChildren(ConsoleConfigurationWorkbenchAdapter.java:37)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.getChildren(BasicWorkbenchAdapter.java:100)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:106)
at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:235)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
Caused by: org.eclipse.core.runtime.CoreException: Unable to restore classpath entry.
at org.eclipse.jdt.launching.JavaRuntime.abort(JavaRuntime.java:1376)
at org.eclipse.jdt.launching.JavaRuntime.abort(JavaRuntime.java:1361)
at org.eclipse.jdt.launching.JavaRuntime.newRuntimeClasspathEntry(JavaRuntime.java:731)
at org.eclipse.jdt.launching.StandardClasspathProvider.recoverRuntimePath(StandardClasspathProvider.java:100)
at org.eclipse.jdt.launching.StandardClasspathProvider.computeUnresolvedClasspath(StandardClasspathProvider.java:65)
at org.eclipse.jdt.launching.JavaRuntime.computeUnresolvedRuntimeClasspath(JavaRuntime.java:1215)
at org.hibernate.eclipse.console.utils.ClassLoaderHelper.getClasspath(ClassLoaderHelper.java:146)
at org.hibernate.eclipse.console.EclipseLaunchConsoleConfigurationPreferences.getCustomClassPathURLS(EclipseLaunchConsoleConfigurationPreferences.java:94)
... 7 more
Caused by: org.xml.sax.SAXException: FWK005 parse may not be called while parsing.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:263)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
at org.eclipse.jdt.launching.JavaRuntime.newRuntimeClasspathEntry(JavaRuntime.java:708)
... 12 more
【问题讨论】:
请出示您的 hibernate-cfg.xml 文件。你真的应该将你的实体命名为Agent
和Client
而不是Agents
和Clients
。
它们是自动生成的,我的表格是复数形式
链接 (607) img59.imageshack.us/img59/5913/ss20120628005009.png
*** 的反垃圾邮件机制完全是垃圾
【参考方案1】:
您忘记在主配置文件中声明映射文件:
<session-factory>
...
<mapping resource="demo/Clients.hbm.xml"/>
<mapping resource="demo/Agents.hbm.xml"/>
</session-factory>
我将在此重复我的评论:实体应以单数形式命名:Agent
、Client
。调整自动生成(参见http://docs.jboss.org/tools/3.0.1.GA/en/hibernatetools/html_single/index.html#tab_and_col),或者在之后重构生成的代码,因为您的所有应用程序代码都将基于这些名称,并且调用Agent
和Agents
真的很糟糕。代码将令人困惑且难以理解。实体无论如何都是应用程序的核心,并且应该包含其他方法,而不仅仅是 setter 和 getter。所以自动生成启动是可以的,但是之后应该忘记了。
【讨论】:
但如果我这样做不一样吗? img339.imageshack.us/img339/1483/ss20120628003928.png ...无论如何我会尝试你的建议 我也试过还要检查 .hbm.xml 文件,该类名完全符合表名。 例如:
<class name="com.test.Emp" table="Emp">.. </class>
【讨论】:
【参考方案3】:在 xxx.hbm.xml
中使用限定类名:package_name.xxx
在 hibernate-mapping 元素中。这是 POJO 的 jboss 规范。
【讨论】:
【参考方案4】:看来我解决了:
1)我不需要修改我原来的 hibernatecfg.xml 。显然,我在控制台配置中定义的映射与我在项目文件夹中的 hbm.xml 文件之间存在不一致(它们的名称略有不同)。
如果您使用 ant 从 hbmxml 文件构建 pojos,则正在阅读此内容的人不应添加 <sessionfactory>
标签(如果我记得的话,会出现双重导入错误,请转到此处
http://docs.jboss.org/tools/latest/en/hibernatetools/html_single/ 并搜索重复)。永远记得重建和刷新您的控制台配置和您的项目
2) select id, ssn FROM demo.Agents 查询有效,但 select id,ssn FROM 代理不...这很奇怪吗?
3)记得在执行之前保存您的查询;似乎否则它会执行较旧的查询
4)如果我的回答有帮助,请留下评论:D,即使它在我发布 10 年后对您有所帮助。我会检查:D
5) 感谢 Nizet 的建议
6) 还有什么我错过的吗?
【讨论】:
关于您的第二个问题:HQL 期望找到类名(完全限定与否),而不是表名。它适用于实体,而不是表。所以应该是 select ... from Agents(大写 A)。以上是关于org.hibernate.MappingException:找不到实体类(使用限定的类名不起作用)的主要内容,如果未能解决你的问题,请参考以下文章