将实体移入外部jar包后未映射

发布于 2024-11-07 23:35:32 字数 521 浏览 0 评论 0原文

在我的 Java/Seam/JbossAS 应用程序中,我决定外部化我的模型类(休眠实体)并将它们移动到另一个项目中。该项目生成 model.jar,然后由主应用程序使用。 model.jar 依赖关系由 Ivy 解决。 使用 Ant 构建主应用程序可以毫无问题。然后我手动将 model.jar 复制到“mainapp.ear/lib”目录中。之后我部署应用程序,没有任何问题(尽管我注意到没有关于找到的映射的日志信息)。但是当我想登录时,我得到了异常:

javax.el.ELException: javax.ejb.EJBTransactionRolledbackException:
    org.hibernate.hql.ast.QuerySyntaxException: AppUser is not
    mapped [select u from AppUser u where u.userName = :usernamePar]

同时没有代码更改,只是将一些类外部化到 jar 中。这是否意味着我在编译主应用程序时需要模型类的源代码?

In my Java/Seam/JbossAS app, I decided to externalize my Model classes (hibernate entities) and moved them into another project. The project produces model.jar, which is then used by the main app. The model.jar dependency is resolved by Ivy.
Building the main app with Ant works without problems. Then I copy manually the model.jar into 'mainapp.ear/lib' directory. Afterwards I deploy the app and there are no problems (although I notice that there are is no log info about found mappings). But when I want to login, I get the exception:

javax.el.ELException: javax.ejb.EJBTransactionRolledbackException:
    org.hibernate.hql.ast.QuerySyntaxException: AppUser is not
    mapped [select u from AppUser u where u.userName = :usernamePar]

There were no code changes in the meantime, just externalizing some of the classes into a jar. Does this mean, that I need the source code of the Model classes when compiling the main app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你げ笑在眉眼 2024-11-14 23:35:32

EntityManagerFactory 专为仅扫描包含 /META-INF/persistence.xml 文件的 jar 中的实体而构建。

为了扫描其他 jar,您必须使用

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
   <persistence-unit name="manager1" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/DefaultDS</jta-data-source>
      <mapping-file>ormap.xml</mapping-file>
      <jar-file>MyApp.jar</jar-file>
      <class>org.acme.Employee</class>
      <class>org.acme.Person</class>
      <class>org.acme.Address</class>
      <shared-cache-mode>ENABLE_SELECTOVE</shared-cache-mode>
      <validation-mode>CALLBACK</validation-mode>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
      </properties>
   </persistence-unit>
</persistence>

请参阅 Hibernate 文档

The EntityManagerFactory is built for scanning entities only from the jar that has a /META-INF/persistence.xml file into.

In order to scan other jars you have to use <jar-file>:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
   <persistence-unit name="manager1" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/DefaultDS</jta-data-source>
      <mapping-file>ormap.xml</mapping-file>
      <jar-file>MyApp.jar</jar-file>
      <class>org.acme.Employee</class>
      <class>org.acme.Person</class>
      <class>org.acme.Address</class>
      <shared-cache-mode>ENABLE_SELECTOVE</shared-cache-mode>
      <validation-mode>CALLBACK</validation-mode>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
      </properties>
   </persistence-unit>
</persistence>

See 2.2.1 Packaging in Hibernate doc.

蓝天 2024-11-14 23:35:32

还要检查您的休眠映射是否正确放置在休眠配置文件中。请注意,hibernate 映射资源或类与 hibernate.cfg.xml 文件的位置相关。

Also check if your hibernate mappings are correctly placed wrt hibernate config file. Note that hibernate mapping resources or classes are relative to the location of hibernate.cfg.xml file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文