多个连接 + JPA + Spring
Multiple Connections + JPA + Spring
我有 2 个数据源连接,我正在指定每个连接应该使用哪个 class,但我仍然遇到一些异常:
这是我的 applicationContext.xml
<context:component-scan base-package="com.example.video"/>
<context:annotation-config/>
<tx:annotation-driven />
<jee:jndi-lookup id="VOD" jndi-name="java:comp/env/jdbc/VOD" />
<jee:jndi-lookup id="SB" jndi-name="java:comp/env/jdbc/SB" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="VOD" />
<property name="packagesToScan" value="com.example.video" />
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactorySB"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="SB" />
<property name="packagesToScan" value="com.example.service" />
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
</bean>
<bean id="transactionManagerSB"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactorySB"/>
</bean>
<jpa:repositories base-package="com.example.video"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager"/>
<jpa:repositories base-package="com.example.service"
entity-manager-factory-ref="entityManagerFactorySB"
transaction-manager-ref="transactionManagerSB"/>
这是我的 persistence.xml
<persistence-unit name="VOD" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.example.video.model.Product</class>
.
.
<class>com.example.video.model.Task</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
<persistence-unit name="SB" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.example.service.model.ChargingInfo</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>
这是个例外:
原因:org.hibernate.HibernateException:缺少 table:CHARGINGINFO
你检查过 MergingPersistenceUnitManager http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.misc.merging-persistence-units
允许您将多个持久性提供程序合并为一个。
发现问题,只需删除 applicationContext.xml
中的行
<
道具键="hibernate.hbm2ddl.auto">验证
从 bean 声明开始它工作正常。
我有 2 个数据源连接,我正在指定每个连接应该使用哪个 class,但我仍然遇到一些异常:
这是我的 applicationContext.xml
<context:component-scan base-package="com.example.video"/>
<context:annotation-config/>
<tx:annotation-driven />
<jee:jndi-lookup id="VOD" jndi-name="java:comp/env/jdbc/VOD" />
<jee:jndi-lookup id="SB" jndi-name="java:comp/env/jdbc/SB" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="VOD" />
<property name="packagesToScan" value="com.example.video" />
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactorySB"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="SB" />
<property name="packagesToScan" value="com.example.service" />
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
</bean>
<bean id="transactionManagerSB"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactorySB"/>
</bean>
<jpa:repositories base-package="com.example.video"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager"/>
<jpa:repositories base-package="com.example.service"
entity-manager-factory-ref="entityManagerFactorySB"
transaction-manager-ref="transactionManagerSB"/>
这是我的 persistence.xml
<persistence-unit name="VOD" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.example.video.model.Product</class>
.
.
<class>com.example.video.model.Task</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
<persistence-unit name="SB" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.example.service.model.ChargingInfo</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>
这是个例外: 原因:org.hibernate.HibernateException:缺少 table:CHARGINGINFO
你检查过 MergingPersistenceUnitManager http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.misc.merging-persistence-units 允许您将多个持久性提供程序合并为一个。
发现问题,只需删除 applicationContext.xml
中的行< 道具键="hibernate.hbm2ddl.auto">验证
从 bean 声明开始它工作正常。