0

我用我的最新配置重写了我的整个问题。我已经多次更改和测试,但仍然没有找到合适的解决方案。

我在我的项目中为 jpa 类增强启用加载时间编织时遇到了困难。

我正在使用Websphere 8.5,而后者又使用openjpa 2.2.2。我有一个Ear文件,其中包含一个和多个包含实体自定义命名War文件的依赖 jar 。JPApersistence.xml

像这样:

MyApp.ear
|-- lib ## contains spring files and other shared libraries
|-- utilJars ## contains my custom made utility jars
|   |-- core.jar ## core-persistence.xml
|   |-- core-api.jar ## core entities
|   |-- mod1.jar ## mod1-persistence.xml
|   |-- mod1-api.jar ## mod1 entities
|   `-- control.jar ## depends on all above jars (in **`MANIFEST.MF`**), 
|                      `contains the main application context (`control-context.xml`) config shown below
`-- MyWeb.war

我在 control.jar 中名为 control-context.xml 的主要上下文文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                           http://www.springframework.org/schema/jee http://schema.spring.io/jee/spring-jee-4.0.xsd
                           http://www.springframework.org/schema/tx http://schema.spring.io/tx/spring-tx-4.0.xsd
                           http://www.springframework.org/schema/util http://schema.spring.io/util/spring-util-4.0.xsd
                           http://www.springframework.org/schema/context http://schema.spring.io/context/spring-context-4.0.xsd">

    <!-- The transactions in this app is driven through annotations -->
    <tx:annotation-driven/>
    <context:load-time-weaver />

    <!-- Defining our datasource here enables us to inject a JPA persistence context using spring -->

    <jee:jndi-lookup id="oneDS" jndi-name="jdbc/oneDS" expected-type="javax.sql.DataSource" cache="true"/>
    <jee:jndi-lookup id="twoDS" jndi-name="jdbc/twoDS" expected-type="javax.sql.DataSource" cache="true"/>

    <!-- =================== -->
    <!--      EMF SETUP      -->
    <!-- =================== -->

    <!-- COMMON EMF PROPERTIES -->
    <bean id="baseEMF" abstract="true" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
        <property name="persistenceProvider">
            <bean class="org.apache.openjpa.persistence.PersistenceProviderImpl" />
        </property>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="jpaPropertyMap" ref="jpaPropertiesMap" />
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.OpenJpaDialect" />
        </property>
    </bean>

    <!-- Merging persistence unit combines all *-persistence.xml files from seperate modules by PU name
         e.g. onePU, twoPU etc.. -->
    <bean id="basePum" class="org.springframework.data.jpa.support.MergingPersistenceUnitManager" abstract="true">
        <!-- store all persistence information in the <module-name>-persistence.xmls located under the custom META-INF
             directory for each module-->
        <property name="persistenceXmlLocation" value="classpath*:META-INF/**/*-persistence.xml" />
        <property name="packagesToScan" value="com.example.myapp.**.model" />
    </bean>

    <!-- COMMON JPA VENDOR ADAPTER PROPERTIES -->
    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
        <property name="database" value="DB2"/>
        <property name="generateDdl" value="false"/>
        <!-- I imagine that this would be turned off in production,
             leaving this here for the time being. -->
        <property name="showSql" value="true"/>
    </bean>

    <!-- COMMON JPA PROPERTIES -->
    <util:map id="jpaPropertiesMap">
        <entry key="openjpa.jdbc.DBDictionary" value="db2" />
        <entry key="openjpa.TransactionMode" value="managed" />
        <entry key="openjpa.ConnectionFactoryMode" value="managed" />
        <entry key="openjpa.DynamicEnhancementAgent" value="true"/>
    </util:map>

    <!-- ONE EMF SETUP -->
    <bean id="oneEMF" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" parent="baseEMF">
        <property name="persistenceUnitName" value="onePU" />
        <property name="persistenceUnitManager">
            <bean class="org.springframework.data.jpa.support.MergingPersistenceUnitManager" parent="basePum">
                <property name="defaultJtaDataSource" ref="oneDS" />
                <property name="defaultPersistenceUnitName" value="onePU" />
            </bean>
        </property>
    </bean>

    <!-- TWO EMF SETUP -->
    <bean id="twoEMF" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" parent="baseEMF">
        <property name="persistenceUnitName" value="twoPU" />
        <property name="persistenceUnitManager">
            <bean class="org.springframework.data.jpa.support.MergingPersistenceUnitManager" parent="basePum">
                <property name="defaultJtaDataSource" ref="twoDS" />
                <property name="defaultPersistenceUnitName" value="twoPU" />
            </bean>
        </property>

    </bean>

    <!-- the default unit of work manager is located at 'java:comp/websphere/UOWManager' -->
    <bean id="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/>

    <!-- Automatically picked up by the spring entity manager factory -->

</beans>

我正在使用 spring 提供的合并持久性单元管理器来组合位于META-INF/<module-name>/<module-name>-persistence.xml每个 jar 文件夹下的自定义持久性单元 xml 中的持久性单元声明。例如META-INF/core/core-persistence.xml

这是一个这样的文件:

<?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://www.w3.org/2001/XMLSchema-instance http://www.w3.org/2001/XMLSchema-instance
             http://java.sun.com/xml/ns/persistence http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
    <persistence-unit name="onePU">
        <class>com.example.myapp.core.model.MyEntity</class>
        <class>com.example.myapp.core.model.MyEntityId</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
    </persistence-unit>
</persistence>

我的实体都有@Entity 和正确的@Id 标签,什么都没有,它们以前一直作为实体工作,我没有修改它们,所以我确定我的实体是有效的。

现在的问题:

我在使用 oneEMF 的 mod1 jar 中设置了 spring rest jpa 资源库:

<jpa:repositories base-package="com.example.myapp.mod1.model.repositories" entity-manager-factory-ref="oneEMF" />

当我在应用程序部署并启动后点击 url 时,我收到以下消息:

Error 500: org.springframework.web.util.NestedServletException: Request processing failed; 
`-- nested exception is org.springframework.orm.jpa.JpaSystemException: No registered metadata for type "class com.example.myapp.core.model.MyEntity". This can happen if this class has not been annotated as a persistent entity or specified in the persistence unit (ex: in the orm.xml). ; 
`-- nested exception is <openjpa-2.2.2-SNAPSHOT-r422266:1462076 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: No registered metadata for type "class com.example.myapp.core.model.MyEntity". This can happen if this class has not been annotated as a persistent entity or specified in the persistence unit (ex: in the orm.xml).

为什么我的实体在加载时没有增强?

我已经浪费了很多时间来尝试配置这个所谓的简单设置,但是我不知道为什么没有对我进行增强。任何帮助甚至同情都将不胜感激。

更新:

在我的战争类文件夹的 META-INF 目录中放置一个虚拟的 persistence.xml 已成功触发实体扫描,但根据我的阅读,如果我使用带有 spring 的 packagesToScan,我不需要persistence.xml。此包扫描是否涉及将由加载时间编织器增强的实体?

感谢您的时间。

4

0 回答 0