8

Is there a way to exclude an Entity in a package using Spring's LocalContainerEntityManagerFactoryBean and Hibernate? I am using spring-data if that matters. I would like to manage the excluded entity with another datasource. I know that you can do it using hibernateSessionFactory (Ignore some classes while scanning PackagesToScan), but I would prefer to use jpa if possible.

Here's my config:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd ">

    <context:property-placeholder location="classpath:app.properties" ignore-unresolvable="true"/>

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${class}" />
        <property name="url" value="${url}" />
        <property name="username" value="${user}" />
        <property name="password" value="${password}" />
    </bean>

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <property name="packagesToScan">
                <value>com.mypackage.new.entities</value>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="hibernate.id.new_generator_mappings">true</prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
            </props>
        </property>
    </bean>

    <!-- Enables the Hibernate @Transactional programming model -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>

    <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>
Community
  • 1
  • 1
rodney757
  • 553
  • 2
  • 7
  • 21
  • 3
    Read this, should help you : http://stackoverflow.com/questions/16293437/ignore-some-classes-while-scanning-packagestoscan – PeaceIsPearl May 08 '16 at 00:08
  • 1
    I've seen that, but is there a way to do it without using the hibernate session factory? I'd prefer to use jpa if possible – rodney757 May 08 '16 at 15:35
  • @rodney757: the second answer in the linked SO post uses JPA in form of `LocalContainerEntityManagerFactoryBean`, isn't that what you desired? – David Artmann Nov 02 '17 at 22:31
  • You can do sub packaging of your beans lest say com.mypackage.new.entities.mySql and com.mypackage.new.entities.Oracle and then use with package. – ajay tomar Apr 13 '18 at 09:53

0 Answers0