简体   繁体   English

OpenJPA 2.x上的实体增强

[英]Entity enhancement on OpenJPA 2.x

I am working on project with Spring and OpenJPA. 我正在使用Spring和OpenJPA进行项目。 I have a use case where I need to create entities of same data source in different projects/maven modules. 我有一个用例,需要在不同的项目/ maven模块中创建具有相同数据源的实体。

Approach 1: 方法1:

I have Maven project core-entities having two modules containing set of entities with specific functionality. 我的Maven项目核心实体有两个模块,其中包含具有特定功能的实体集。

Module A - Enitity1, Enitity2, Enitity3 Module B – Enitity4, Enitity5, Enitity6 模块A-Enitity1,Enitity2,Enitity3模块B – Enitity4,Enitity5,Enitity6

I have a created separate persistence units and persistence xml for each set of entities. 我为每组实体创建了一个单独的持久性单元和持久性xml。 It works fine when entities are enhanced in their respective maven modules. 当实体在各自的Maven模块中得到增强时,它可以正常工作。

Approach 2: In this approach I have separate modules for entities as above but the only difference is that I am using a single persistence unit for both set of entities. 方法2:在这种方法中,我为上述实体提供了单独的模块,但是唯一的区别是,我对两组实体都使用了一个持久性单元。 I have added the openjpa-maven-plugin for enhancing entities in both the modules. 我添加了openjpa-maven-plugin来增强两个模块中的实体。 In this case when entities are enhanced using maven plugin it gives exception saying no metadata found for entities in Module A at runtime. 在这种情况下,当使用maven插件增强实体时,它会给出异常,说明在运行时未在模块A中找到实体的元数据。 Is it because when the entities are enhanced, somehow metadata of entities in Module A is lost at runtime.This problem does not come with entities of module B. 是因为增强实体后,模块A中的实体元数据在运行时会以某种方式丢失。模块B的实体不会出现此问题。

I have few questions around this issue: 我对此问题有几个疑问:

1)Is it recommended to have multiple persistence units for same data source (as done in Approach 1)? 1)是否建议对同一数据源使用多个持久性单元(如方法1所述)?

2)Is there any way by which I can enhance entities in single persistence unit and different maven modules? 2)有什么方法可以增强单个持久性单元和不同的Maven模块中的实体?

3)Are the any alternate approaches of entities enhancement other than using eclipse and maven plugin? 3)除了使用eclipse和maven插件之外,是否还有其他增强实体的方法?

Below is the plugin in pom.xml of each module: 以下是每个模块的pom.xml中的插件:

<build>
        <plugins>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>openjpa-maven-plugin</artifactId>
                <version>1.1</version>
                <configuration>
                    <includes>**/entity/**/*.class</includes>
                    <addDefaultConstructor>true</addDefaultConstructor>
                    <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
                    <persistenceXmlFile>${basedir}/src/main/resources/META-INF/moduleApersistence.xml</persistenceXmlFile>
                </configuration>
                <executions>
                    <execution>
                        <id>enhancer</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>enhance</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.openjpa</groupId>
                        <artifactId>openjpa</artifactId>
                        <version>${openjpa.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings 
                    only. It has no influence on the Maven build itself. -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            org.apache.openjpa
                                        </groupId>
                                        <artifactId>
                                            openjpa-maven-plugin
                                        </artifactId>
                                        <versionRange>
                                            [2.2.0,)
                                        </versionRange>
                                        <goals>
                                            <goal>enhance</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            org.codehaus.mojo
                                        </groupId>
                                        <artifactId>
                                            cobertura-maven-plugin
                                        </artifactId>
                                        <versionRange>
                                            [2.5.2,)
                                        </versionRange>
                                        <goals>
                                            <goal>instrument</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

It's generally a good idea to group functional concepts into modules. 将功能性概念分组到模块中通常是一个好主意。 Following that idea, it makes sense that the classes describing the functional concepts of ModuleA are contained within ModuleA, not spread according to their technical implementation. 遵循该想法,有意义的是,描述ModuleA功能概念的类包含在ModuleA中,而不是根据其技术实现而扩展。

  1. Single or multiple persistence units: It depends on your application and dependencies between your domain objects. 单个或多个持久性单元:这取决于您的应用程序和域对象之间的依赖关系。 Each persistence unit will have its own 1st level cache (read more here ). 每个持久性单元将具有自己的第一级缓存( 在此处了解更多信息 )。 Summarized in one line, this means that a row representing Entity4 will be mapped to different instances within the same transaction depending on which persistence unit was queried. 总结起来,这意味着代表Entity4行将根据查询的持久性单元而映射到同一事务中的不同实例。 For instance entityManager.find(Entity4.class, 3L) in ModuleB will return a different instance than the one you would get in a OneToMany in Entity3 in ModuleA since Entity3 and Entity4 are in different persistence units. 例如entityManager.find(Entity4.class, 3L)ModuleB会比一个,你会得到一个返回不同的实例OneToManyEntity3ModuleA因为ENTITY3和Entity4在不同的持久化单元。

    In general, you keep entities that have nothing to do with each other in different persistence units, and entities that are correlated somehow in the same persistence unit. 通常,将没有任何关系的实体保留在不同的持久性单元中,并将以某种方式关联的实体保留在同一持久性单元中。 My advise is to keep to one persistence unit unless you're sure you'll benefit from several. 我的建议是保留一个持久性单元,除非您确定将从多个持久性单元中受益。 Keep it simple, avoid complexity if you can. 保持简单,尽可能避免复杂。

  2. Yes. 是。 Place your persistence.xml in a separate module. persistence.xml放在单独的模块中。 Configure the openjpa-maven-plugin in the parent pom and then load it in the modules containing entities. 在父pom中配置openjpa-maven-plugin ,然后将其加载到包含实体的模块中。

parent-pom.xml: parent-pom.xml:

  <pluginManagement><plugins> ... <plugin> <groupId>org.apache.openjpa</groupId> <artifactId>openjpa-maven-plugin</artifactId> <version>${openjpa-maven-plugin.version}</version> <executions> <execution> <id>enhancer</id> <phase>process-classes</phase> <goals> <goal>enhance</goal> </goals> </execution> </executions> <configuration> <addDefaultConstructor>true</addDefaultConstructor> <enforcePropertyRestrictions>true</enforcePropertyRestrictions> <persistenceXmlFile>${project.parent.basedir}/module-infra/src/main/resources/META-INF/persistence.xml</persistenceXmlFile> </configuration> <dependencies> <dependency> <groupId>org.apache.openjpa</groupId> <artifactId>openjpa</artifactId> <version>${openjpa.version}</version> </dependency> </dependencies> </plugin> 

ModuleA and ModuleB pom.xml: ModuleA和ModuleB pom.xml:

  <build><plugins> <plugin> <groupId>org.apache.openjpa</groupId> <artifactId>openjpa-maven-plugin</artifactId> <configuration> <includes> **/*SomeEntity.class </includes> </configuration> </plugin> 

module-infra/../persistence.xml (configures a non-jta datasource in an osgi container) module-infra /../ persistence.xml(在osgi容器中配置非jta数据源)

  <?xml version="1.0" encoding="UTF-8"?> <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="yourPersistenceUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <non-jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=somedb)</non-jta-data-source> <class>com.example.modulea.Entity1</class> <class>com.example.moduleb.Entity3</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="openjpa.TransactionMode" value="local"/> <property name="openjpa.ConnectionFactoryMode" value="local"/> <property name="openjpa.Log" value="slf4j"/> </properties> </persistence-unit> </persistence> 

  1. Not that I know of. 从来没听说过。 The maven plugin should suffice though. Maven插件应该足够了。 I've never used the eclipse plugin. 我从未使用过eclipse插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM