0

I have two databases which I want to access from my Spring application. I configured two SharedEntityManagerBean for both databases. Here is the config:

<jpa:repositories base-package="xxx" entity-manager-factory-ref="entityManagerFactory1" />
<jpa:repositories base-package="xxx" entity-manager-factory-ref="entityManagerFactory2" />
<tx:annotation-driven/>

<bean class="org.springframework.orm.jpa.JpaTransactionManager"
        id="transactionManager1">
        <property name="entityManagerFactory" ref="entityManagerFactory1" />
        <property name="dataSource" ref="dataSource1" />
</bean>

<bean class="org.springframework.orm.jpa.JpaTransactionManager"
        id="transactionManager2">
        <property name="entityManagerFactory" ref="entityManagerFactory2" />
        <property name="dataSource" ref="dataSource2" />
</bean>


<bean id="entityManagerFactory1"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:packagesToScan="xxxxxx"
        ....
</bean>

<bean id="entityManagerFactory2"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        p:packagesToScan="xxxxx"
        ...
</bean>


<bean id="entityManager1" class="org.springframework.orm.jpa.support.SharedEntityManagerBean" >            
        <property name="entityManagerFactory" ref="entityManagerFactory1" />
</bean>

<bean id="entityManager2" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="entityManagerFactory2" />
</bean>


<bean id="dataSource1"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
...
</bean>

<bean id="dataSourceOntology"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
...
</bean>

I have two EntityLocators which are not managed by Spring that access entities in each of the corresponding databases. They look something like that:

public class SpringEntitiyLocator1 {

    private EntityManager em;

    public SpringEntitiyLocator1() {

    }

    private EntityManager getEM() {
        if (em == null) {
            ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(RequestFactoryServlet.getThreadLocalServletContext());
            SharedEntityManagerBean bean =  context.getBean("entityManager1",SharedEntityManagerBean.class);
            em = bean.getObject();
        }
        return em;
    }
}

When I have only one SharedEntityManagerBean defined in my applicationContext the call to getBean() works fine. However once I have both SharedEntityManagerBeans defined I get the error:

Bean named 'x' must be of type [y], but was actually of type [$Proxy]

I read on SO that I should use cglib proxying by adding <aop:config proxy-target-class="true"/> to my application.xml.

Is that the best solution ?

When I add that line I get Caused by: java.lang.NoClassDefFoundError: org/aspectj/util/PartialOrder$PartialComparable errors.
Do I need aspectj for that?

EDIT:

In case I have only one SharedEntityManagerBean defined I can call getBean(SharedEntityManagerBean.class). This works fine.
I debugged the code and it seems that this call will call getBean("&entityManager1",SharedEntityManagerBean.class) (note &) .

However when I pass the name getBean("EntityManager1",SharedEntityManagerBean.class) I get a type cast exception.

Having both SharedEntityManagerBeans defined and call getBean without a name also causes an exception (can't find a bean with that name).

So my current workaround is to call: getBean("&entityManager1",SharedEntityManagerBean.class) and getBean("&entityManager2",SharedEntityManagerBean.class)

This works fine.

Ümit
  • 17,379
  • 7
  • 55
  • 74

1 Answers1

0

Ok apparently SharedEntityManagerBean is a FactoryBean and for that I have to add & before the bean name to retrieve the SharedEntityManagerBean.

Alternatively I could probably just call:

em = context.getBean("entityManager",EntityManager.class);

See here and here for reference.

Community
  • 1
  • 1
Ümit
  • 17,379
  • 7
  • 55
  • 74