简体   繁体   English

不同数据源的JPA MetaDataException

[英]JPA MetaDataException for different datasources

I have the problem with JPA Criteria API while using in my project different datasource persistance. 在我的项目中使用不同的数据源持久性时,我遇到了JPA Criteria API的问题。

There are two PU uses different datasources: 有两种PU使用不同的数据源:

<persistence-unit name="analysis" transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <non-jta-data-source>AnalysisDS</non-jta-data-source>
    <class>entity1</class>
    <class>entity2</class>
    <class>entity3</class>

and

<persistence-unit name="reaction" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <non-jta-data-source>ReactionDS</non-jta-data-source>
    <class>someEntity1</class>
    <class>someEntity2</class>
    <class>someEntity3</class>

Spring load it, in applicationContext Spring在applicationContext中加载它

<bean id="defaultAnalysysDataSource"
      class="org.springframework.jndi.JndiObjectFactoryBean"
      lazy-init="default">
    <property name="jndiName" value="AnalysisDS"/>
    <property name="lookupOnStartup" value="false"/>
    <property name="cache" value="true"/>
    <property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>

<bean id="defaultReactionDataSource"
      class="org.springframework.jndi.JndiObjectFactoryBean"
      lazy-init="default">
     <property name="jndiName" value="ReactionDS"/>
     <property name="lookupOnStartup" value="false"/>
     <property name="cache" value="true"/>
     <property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>

In my DAO I can work with this PU with EntityManager, for example for ReactionDS I'Am using 在我的DAO中,我可以使用带有EntityManager的此PU,例如,对于我使用的ReactionDS

 @PersistenceContext(unitName = "reaction")
 private EntityManager entityManager;

And all work done - simple query's and JPQL expressions. 并完成了所有工作-简单的查询和JPQL表达式。 But when I want to introduce to my DAO JPA Criteria API Like this : 但是当我想向我的DAO JPA Criteria API进行介绍时:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
...

I have exception after getCriteriaBuilder() method works: getCriteriaBuilder()方法工作后出现异常:

Caused by: <openjpa-2.4.0-r422266:1674604 fatal user error> org.apache.openjpa.util.MetaDataException: Errors encountered while resolving metadata.  See nested exceptions for details.
    at org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:675)
    at org.apache.openjpa.meta.MetaDataRepository.getMetaDataInternal(MetaDataRepository.java:418)
    at org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:389)
    at org.apache.openjpa.persistence.meta.MetamodelImpl.(MetamodelImpl.java:86)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.getMetamodel(EntityManagerFactoryImpl.java:348)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.getCriteriaBuilder(EntityManagerFactoryImpl.java:332)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

...
Caused by: <openjpa-2.4.0-r422266:1674604 fatal user error> org.apache.openjpa.util.MetaDataException: Table "ANALYSIS.ENTITY1" given for "entity1" does not exist.
    at org.apache.openjpa.jdbc.meta.MappingInfo.createTable(MappingInfo.java:532)
    at org.apache.openjpa.jdbc.meta.ClassMappingInfo.getTable(ClassMappingInfo.java:317)
    at org.apache.openjpa.jdbc.meta.ClassMappingInfo.getTable(ClassMappingInfo.java:339)
    at org.apache.openjpa.jdbc.meta.strats.FullClassStrategy.map(FullClassStrategy.java:73)
    at org.apache.openjpa.jdbc.meta.ClassMapping.setStrategy(ClassMapping.java:392)
    at org.apache.openjpa.jdbc.meta.RuntimeStrategyInstaller.installStrategy(RuntimeStrategyInstaller.java:55)
    at org.apache.openjpa.jdbc.meta.MappingRepository.prepareMapping(MappingRepository.java:410)
    at org.apache.openjpa.meta.MetaDataRepository.preMapping(MetaDataRepository.java:769)
    at org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:658)
    ... 147 more

The problem root cause in JPA, because his trying to use a tables from Analys in Reaction PU and extracts all meta-classes for entities that are located in different datasources, but access to them is doing in one. JPA中问题的根本原因在于,因为他试图使用来自Response PU中的Analys中的表并提取位于不同数据源中的实体的所有元类,但是对它们的访问却是一个集合。

But when I granted select on Entity1 to ReactionDS - all works done. 但是当我在Entity1上授予对ResponseDS的选择时,所有工作都完成了。 (because I can use Select * from Analysis.Entity1 from reaction) (因为我可以从反应中使用Analysis.Entity1中的Select *)

The question - how to make the metamodel classes to choose working only within the specified DS in EntityManager (in current example - Reaction, not together with Analysis) ? 问题-如何使元模型类选择仅在EntityManager中的指定DS中工作(在当前示例中-反应,不与Analysis一起使用)?

ps Database is Oracle, using Weblogic 12.1.3 and OpenJpa2.4. ps数据库是Oracle,使用Weblogic 12.1.3和OpenJpa2.4。 Metamodel is generated automatically with maven plugin on compile: 使用Maven插件在编译时自动生成元模型:

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <executions>
        <execution>
            <id>process</id>
            <goals>
                <goal>process</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <processors>
                    <processor>org.apache.openjpa.persistence.meta.AnnotationProcessor6</processor>
                </processors>
                <optionMap>
                    <openjpa.metamodel>true</openjpa.metamodel>
                </optionMap>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.apache.openjpa</groupId>
            <artifactId>openjpa</artifactId>
            <version>${openjpa.version}</version>
        </dependency>
    </dependencies>
</plugin>

I think you may be confused by your Spring Framework datasource declarations. 我认为您可能会对Spring Framework数据源声明感到困惑。

These beans do not define your datasource, they only provide a way for other Spring components to access the datasources that have been configured in your server. 这些bean并未定义您的数据源,它们只是为其他Spring组件提供了一种访问服务器中已配置的数据源的方式。 JPA does not use these at all. JPA根本不使用这些。

Therefore, your problem lies in the datasources that you have defined in your WebLogic server. 因此,您的问题出在您在WebLogic服务器中定义的数据源。 It looks like you have defined both datasources to reference the same database instance. 看起来您已经定义了两个数据源以引用同一数据库实例。

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

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