将实体移动到外部jar包后未映射的实体

时间:2011-05-17 15:19:02

标签: java hibernate jar entity seam

在我的Java / Seam / JbossAS应用程序中,我决定外化我的Model类(hibernate实体)并将它们移动到另一个项目中。该项目生成了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。这是否意味着,在编译主应用程序时我需要Model类的源代码?

2 个答案:

答案 0 :(得分:5)

EntityManagerFactory仅用于从具有/META-INF/persistence.xml文件的jar中扫描实体。

要扫描其他罐子,您必须使用<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>

Hibernate doc中的2.2.1打包。

答案 1 :(得分:0)

同时检查你的hibernate映射是否正确放置在hibernate配置文件中。请注意,hibernate映射资源或类是相对于hibernate.cfg.xml文件的位置。