multi-module project spring 3.0 AnnotationSessionFactoryBean packagesToScan property value to be set module-wise

StackOverflow https://stackoverflow.com/questions/8536191

Domanda

I have the following structure:

common-module

  • Contains the common module related , services and persistence api
  • Contains a common persistence context test-common-persistence-context.xml.

search-module (depends on Common Module)

  • Contains the search module related model beans(marked with JPA Entity annotations), services and persistence api
  • Contains search module related spring context files

booking-module (depends on Common Module) - Contains the booking module related model beans(marked with JPA Entity annotations), services and persistence api - Contains booking module related spring context files

In the common-module I have test-common-persistence-context.xml.For the sessionFactory bean having type org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean I need to set the property "packagesToScan" value to the packages in which JPA Entity annotation marked model beans are present.Without it, I get the exception Unknown entity: MY_ENTITY_NAME

Common Module: test-common-persistence-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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

            <!-- Property Place Holder -->
            <context:property-placeholder location="classpath:test-common-persistence-bundle.properties" />

            <!-- Data Source -->        
            <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverClassName" value="${test.db.driverClassName}"/>
                <property name="url" value="${test.db.jdbc.url}"/>
                <property name="username" value="${test.db.username}"/>
                <property name="password" value="${test.db.password}"/>
            </bean>

            <!--
                Hibernate Configuration
            --> 
            <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
                <property name="dataSource" ref="dataSource"/>
                <property name="packagesToScan" value="${test.packages.to.scan.jpa.annotations}"/>
                <property name="hibernateProperties">
                    <value>
                        <!-- SQL dialect -->
                        hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
                        hibernate.hbm2ddl.auto=update
                    </value>
                </property>      
            </bean>

            <!-- Transaction Manager -->
            <bean id="txManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory" ref="sessionFactory"/>
            </bean>

            <tx:annotation-driven transaction-manager="txManager"/>

    </beans>

In my common-module there are no JPA entities so I don't have any package to scan so I keep the "packagesToScan" property value empty

However I need to use the same persistence context in my search-module and booking-module tests (SearchPersistenceTestBase.java) so that the JPA entities in the respective module gets detected.

Search Module: SearchPersistenceTestBase.java

    @Ignore
    @ContextConfiguration(locations = {
            "classpath:test-common-persistence-context.xml",
            "classpath:test-search-spring-context.xml"})
    @TransactionConfiguration(transactionManager="txManager", defaultRollback=true)
    public class SearchPersistenceTestBase extends AbstractTransactionalJUnit4SpringContextTests {

    }

Can anybody please guide me in how to achieve this desired behavior with the set up I have shown above?

** Approach I tried **

I thought of using an additional java.lang.String type bean whose value is set from a properties file

 <bean id="entityPackagesToScan" class="java.lang.String">
     <constructor-arg value="${test.packages.to.scan.jpa.annotations}" />
  </bean>

where test.packages.to.scan.jpa.annotations is defined as empty in test-common-persistence-bundle.properties

And then I override the bean definition in test-search-spring-context.xml

test-search-spring-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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        <!-- Property Place Holder -->
        <context:property-placeholder location="classpath:test-search-bundle.properties" />

        .. context-component scan elements here

        <bean id="entityPackagesToScan" class="java.lang.String">
            <constructor-arg value="${test.packages.to.scan.jpa.annotations}" />
        </bean>
</beans>

where test.packages.to.scan.jpa.annotations is defined as "com.search.model" in test-search-bundle.properties

But this didn't worked and I got the exception Unknown entity: MY_ENTITY_NAME

Thanks,
Jignesh

È stato utile?

Soluzione

Ok I got this resolved. org.springframework.beans.factory.config.PropertyOverrideConfigurer is what was needed.I am posting here the solution for reference in case anybody faces similar problem like I mentioned in first post:

Common Module: test-common-persistence-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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

            <!-- Property Place Holder -->
            <context:property-placeholder location="classpath:test-common-persistence-bundle.properties" />

            <!-- Data Source -->        
            <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverClassName" value="${test.db.driverClassName}"/>
                <property name="url" value="${test.db.jdbc.url}"/>
                <property name="username" value="${test.db.username}"/>
                <property name="password" value="${test.db.password}"/>
            </bean>

            <!--
                Hibernate Configuration
            --> 
            <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
                <property name="dataSource" ref="dataSource"/>
                <!-- Not setting "packagesToScan" property here.As common-module doesn't contain any JPA entities -->
                <property name="hibernateProperties">
                    <value>
                        <!-- SQL dialect -->
                        hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
                        hibernate.hbm2ddl.auto=update
                    </value>
                </property>      
            </bean>

            <!-- Transaction Manager -->
            <bean id="txManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory" ref="sessionFactory"/>
            </bean>

            <tx:annotation-driven transaction-manager="txManager"/>

    </beans>

Search Module: test-search-spring-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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        .. context-component scan elements here

        <!-- 
        Holds the overridden value for the org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
        (id="sessionFactory" bean in test-common-persistence-context.xml (common module))
        "packagesToScan" property value set for search module.

        E.g. sessionFactory.packagesToScan=com.search.model.persistent 
        -->
    <context:property-override location="classpath:test-search-bundle.properties"/>
</beans>

Search module: test-search-bundle.properties

     ########### Packages to scan JPA annotation
     # This is used by org.springframework.beans.factory.config.PropertyOverrideConfigurer
     # to override   org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean's
     # packagesToScan property value which is different for each module i.e. 
     # common-module does not contain any persistent models
     # search-module and booking-module contains persistent models
     # which needs to be scanned by Spring container to detect them as JPA entities

     sessionFactory.packagesToScan=com.search.model.persistent

Thanks,
Jignesh

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top