Inject multiple datasources to single EntityManagerFactory?

Viewed 97

I am trying to fetch data from two different tables of two different schemas (logical db) in same database server using innerjoin query +JPA nativesql. How can I inject multiple datasources to same entity manager?

my config file looks like this

<bean id="userDataSource" class="org.jdbcdslog.DataSourceProxy">
    <description>Data source for User database</description>
    <property name="targetDSDirect">
        <bean class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="jdbc/cUser" />
            <property name="resourceRef" value="true" />
        </bean>
    </property>
</bean>

<bean id="masterDataSource" class="org.jdbcdslog.DataSourceProxy">
    <description>Data source for User database</description>
    <property name="targetDSDirect">
        <bean class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="jdbc/Master" />
            <property name="resourceRef" value="true" />
        </bean>
    </property>
</bean>

<bean id="entitymanager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation"
              value="classpath:com/jpa_persistence.xml" />
    <property name= "persistenceUnitName" value= "CP"/>
    <property name="dataSource" ref="userDataSource" />   
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    <property name="jpaPropertyMap">
        <map>
            <entry key="eclipselink.weaving" value="false"/>
        </map>
    </property>  
</bean>
1 Answers

Most of the database engines i know do not required these kind of evil double datasource tricks, you can just grant read (or write) access to both schema on the same user.

This way the user will have access to both of those schemas and be able to cross query.

Finally use the Entity anotation to define which schema to use

@Entity @Table(name = "author", schema = "bookstore") public class Author { ... }

Related