I have the problem with JPA Criteria API while using in my project different datasource persistance.
There are two PU uses different datasources:
<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
<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
@PersistenceContext(unitName = "reaction")
private EntityManager entityManager;
And all work done - simple query's and JPQL expressions. But when I want to introduce to my DAO JPA Criteria API Like this :
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
...
I have exception after getCriteriaBuilder() method works:
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.
But when I granted select on Entity1 to ReactionDS - all works done. (because I can use Select * from Analysis.Entity1 from reaction)
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) ?
p.s Database is Oracle, using Weblogic 12.1.3 and OpenJpa2.4. Metamodel is generated automatically with maven plugin on compile:
<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>