Ajouter un paquet supplémentaire pour numériser à l'entité gestionnaire de l'aide de configuration Spring?

J'ai un Spring Batch application qui a un Ressort contexte de configuration qui, normalement, chaque lot de travail de référence. De cette manière, chaque lot de travail utilise la même entité gestionnaire.

batch-context.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <!-- ... -->

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="myPersistenceUnit" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="packagesToScan" value="com.example.domain" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider</prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.jdbc.fetch_size">100</prop>
                <prop key="hibernate.jbc.batch_size">1000</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.use_sql_comments">false</prop>
            </props>
        </property>
    </bean>

    <!-- ... -->

</beans>

Maintenant dans mon travail par lots contexte (appelons-la ExampleBatch.xml je veux ajouter un autre paquet d'analyse de la déjà défini entityManagerFactory bean. Est-ce possible?

ExampleBatch.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">

    <!-- ... -->    

    <import resource="classpath:batch-context.xml" />

    <bean id="entityManagerFactoryWithExtraPackages"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        parent="entityManagerFactory">

        <!-- How do I override the packagesToScan property on the already defined entityManagerFactory bean?-->
        <property 
            name="packagesToScan" 
            value ="com.example.domain,com.example.domain.abstraction"
        />
    </bean>

    <!-- ... -->

</beans>

La façon que j'ai maintenant ne fonctionnera pas car il se plaint que "No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2"

Est d'essayer de remplacer le "packagesToScan" bien la bonne démarche à adopter dans ce scénario? Est-il un meilleur moyen d'atteindre ce comportement?

Edit:

J'ai été en mesure d'accomplir ce que j'avais besoin de l'aide de l' property-override fonctionnalité. Ci-dessous la mise à jour ExampleBatch.xml que je suis allé avec

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">

    <!-- ... -->    

    <import resource="classpath:batch-context.xml" />

    <context:property-override properties-ref="entityManagerOverride"/>

    <bean id="entityManagerOverride"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="properties">
            <util:properties>
                <prop key="entityManagerFactory.packagesToScan">com.example.domain,com.example.batch.module.domain</prop>
            </util:properties>
        </property>
    </bean>

    <!-- ... -->

</beans>

La mesure de Printemps ne pas crier à moi que c'est une configuration non valide. N'ont pas encore déterminé si ce est en fait de produire le résultat souhaité.

Edit 2:

Le bien-remplacer la méthode ne semble pas être suffisant. C'est une configuration valide, mais après avoir inspecté le gestionnaire de l'entité au moment de l'exécution comme ceci:

for (EntityType<?> entity : manager.getMetamodel().getEntities()) {
    String name = entity.getName();
    System.out.println(name);
}

Il ne contient que des entités de la com.exemple.domaine paquet.

Quelqu'un a d'autres idées?

Cette question a aidé à me conduire à ma solution: stackoverflow.com/questions/12382070/...

OriginalL'auteur FGreg | 2013-01-16