0

我正在寻找一种简单的方法来扩展现有的 JPA 映射。思路如下:

我有一个带有 EJB3+JPA 模块的 EAR 项目,它有 ClassA 注释并映射到表 class_a。但我希望其他一些模块(另一个 EJB 模块)具有 ClassB,它为 ClassA 添加更多属性(扩展?)。

我想到的一种方法是将这些附加字段添加到 class_a 表并运行非 HQL 查询来检索该数据。这不好,因为我必须手动做很多事情:类型映射,列映射等。

我做了简单的检查,但似乎我无法在第二个模块中扩展 ClassA(通过 ClassB),因为它们使用不同的 EntityManagerFactories 并且第二个模块看不到第一个模块中的某些类,反之亦然。

我已经在persistence.xml 中看到了<jar-file> 标记。我需要类似的东西,但是使用它需要在第一个模块中列出该文件并且它必须存在(如果找不到它不会跳过)。有没有类似的东西可以放在扩展模块(第二个)而不是可扩展模块(第一个)上?

如果有一种方法可以在运行时扩展 JPA 映射,那就太好了。有没有这样的方法?我的问题还有其他解决方案吗?

4

1 回答 1

0

实现的解决方案如下。我有一个 jar 和两个 EJB 模块:

  1. 罐子是基础罐子。它包含扩展的基本实体和本地接口:

    @Entity
    public class BaseEntity {
        public long id;
        @Id @GeneratedValue
        public long getId() {...
        ... other variables, getters, setters ...
    }
    
    @Local
    public interface EntitiyManagerWithExtendedEntitiesInterface {
        public EntityManager getEntityManager;
    }
    
  2. 第一个 EJB 模块将扩展基本实体并添加 EJB 以获得它的实体管理器。该模块还包括persistence.xml<jar-file>../path_to_first_jar_file.jar</jar-file>线。

    @Entity
    ... discriminator annotations
    public class ExtEntity extends BaseEntity {
        ... additional fields here
    }
    
    @Stateless
    public class EntitiyManagerWithExtendedEntitiesBean implements EntitiyManagerWithExtendedEntitiesInterface {
        @PersitenceContext
        EntityManager em;
        public EntityManager getEntityManager() {
            return em;
        }
    }
    
  3. 第二个 EJB 模块将具有只需要 jar 编译但需要第一个 EJB 运行的 EJB(需要一个将实现EntitiyManagerWithExtendedEntitiesInterface接口的 EJB)。

    @Stateless
    public class getSomeEntity {
        @EJB
        EntitiyManagerWithExtendedEntitiesInterface ext;
        EntityManager em;
        @PostConstruct
        public void injectEntityManager() {
            em = ext.getEntityManager();
        }
        public void ejbInterfaceMethod() {
            ... method that uses em variable (EntityManager)
        }
    }
    

这样,应用程序服务器将不得不管理模块之间的依赖关系,并且我可以轻松地交换第一个 EJB 模块以包含另一组扩展实体。

于 2009-06-19T07:13:37.290 回答