本文介绍了在运行时动态添加实体类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个要求在运行时将实体类添加到持久性单元,而不是在persistence.xml中指定所有实体类。有人能帮我一样吗?

I have this requirement to add entity classes to the persistence unit at runtime rather than specifying all of them in the persistence.xml. Can someone help me with the same?

我知道Hibernate有自己的相同机制:

I am aware that Hibernate has its own mechanism of doing the same using:

AnnotationConfiguration.addAnnotatedClass(Class)等 - 您还可以通过编程方式添加hibernate config( * .hbm.xml )文件。

AnnotationConfiguration.addAnnotatedClass(Class), etc - You can also add hibernate config (*.hbm.xml) files programmatically.

要求是不重新启动应用服务器,我应该能够动态地将实体类/它们的配置(映射)文件添加到持久性单元。

The requirement is that without restarting the app server, I should be able to keep adding entity classes / their config (mapping) files to the persistence unit dynamically.

但是,在运行时以编程方式将实体类/配置文件添加到持久性单元的解决方案不应该特定于JPA实现。

But the solution to programmatically add entity classes / configuration files at runtime to the persistence unit should not be specific to a JPA implementation.

推荐答案

JPA尚未提供此功能。您可以查看以下三个选项:

JPA doesn't offer this feature yet. Here are three options you can check out :




  • 关于SO的这个问题与您的类似。一个答案报告说它对Spring来说是可行的。

  • Programmatically loading Entity classes with JPA 2.0?
    This question on SO is similar to yours. An answer reports that it feasible with Spring.



这里给出了很好的指针。

JPA 2.0: Adding entity classes to PersistenceUnit *from different jar* automatically
Good pointers are given here.

最后但并非最不重要的,一个简单的解决方法:

1。生成 persistence.xml 动态(简单的XML文件创建),带有新的持久性单元。

2。动态地将持久性文件添加到类路径中( URLCLassLoader

3. 要求 PersistenceProvider 加载新的持久性单元( createEntityManagerFactory

Last but not least, a simple work around :
1. Generate a persistence.xml on the fly (simple XML file creation) with a new persistence unit.
2. Add persistence file to classpath dynamically (URLCLassLoader)
3. Ask PersistenceProvider to load new persistence unit (createEntityManagerFactory)

编辑:

如果JPA提供程序是Hibernate,那么从Hibernate 4.0开始,就可以直接将实体传递给提供程序而不在 persistence.xml中声明它们 文件。 Hibernate将动态处理实体。

If the JPA provider is Hibernate, since Hibernate 4.0, it's possible to pass directly entities to this JPA provider without declaring them in the persistence.xml file. Hibernate will handle the entities on the fly.

编辑:

这是JPA 2.1 + Hibernate 4.3.7.Final的示例配置,没有声明任何实体:

Here is a sample configuration of JPA 2.1 + Hibernate 4.3.7.Final without declaring any entities :

META-INF / persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
    version="2.1">

    <persistence-unit name="my-persistence-unit"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <!-- Database Properties -->
            <property name="javax.persistence.jdbc.url"
                value="jdbc:postgresql://localhost:5432/my-database" />
            <property name="javax.persistence.jdbc.user" value="login" />
            <property name="javax.persistence.jdbc.password" value="password" />

            <!-- Hibernate Properties -->
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.default_schema" value="public" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="true" />

            <!-- Connection Pool -->
            <property name="hibernate.c3p0.min_size" value="5" />
            <property name="hibernate.c3p0.max_size" value="20" />
            <property name="hibernate.c3p0.timeout" value="500" />
            <property name="hibernate.c3p0.max_statements" value="50" />
            <property name="hibernate.c3p0.idle_test_period" value="2000" />
        </properties>
    </persistence-unit>

</persistence>

参考




  • JPA 2.1 Specs : 8.2 Persistence Unit Packaging
  • JPA 2.1 Specs : 8.2.1.6 mapping-file, jar-file, class, exclude-unlisted-classes

这篇关于在运行时动态添加实体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:40
查看更多