Spring数据jpa + Spring Transaction为多个数据库

时间:2016-06-20 13:19:43

标签: spring spring-data-jpa spring-transactions jboss6.x spring-repositories

我使用带有@Transaction的spring数据jpa为我的服务实现多个事务(数据库),具有两个持久的Unit名称。并在Jboss 6.4 EAP中部署

spring xml包含

<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"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd
             http://www.springframework.org/schema/data/jpa
             http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
             http://www.springframework.org/schema/tx 
             http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
             http://www.springframework.org/schema/context 
             http://www.springframework.org/schema/context/spring-context-3.1.xsd
             http://www.springframework.org/schema/mvc
             http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
             http://www.springframework.org/schema/jee 
             http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">

    <context:annotation-config />
    <tx:annotation-driven />
    <context:component-scan base-package="com.test.demo.domain.service" />
    <context:component-scan base-package="com.test.demo.domain" />
    <bean id="entityManagerFactory"
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="demo-domain" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
        </property>
        <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
    </bean>

    <bean id="entityManagerFactoryMeta"
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="demo-meta" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
        </property>
        <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
    </bean>
    <bean id="persistenceExceptionTranslationPostProcessor"
              class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <qualifier value="demoadmin"/>
    </bean>
    <bean id="transactionManagerMeta" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoryMeta" />
        <qualifier value="demoappmeta"/>
    </bean>

    <jpa:repositories base-package="com.test.demo.domain.repository" />

</beans>

以下是我的服务详情

@Named(ServerConstants.SERVICE_DRAG_DROP)
**@Transactional("demoappmeta")**
public class DragDropService {
    private static final Logger LOG = LoggerFactory.getLoggerFactory().getDomainLogger(ServerConstants.LOGGER_DOMAIN, DragDropService.class.toString());

    @Inject
    private TbAsmiDragDropRepository dragDropRepo;

    public void createDragDrop(Message pMessage){
        JSONObject requestJson = pMessage.getRequestObject().getRequestJson();
        LOG.debug("inside createDragDrop() - Request : "+requestJson);

在Jboss中我无法部署它,我得到以下异常:

  

引起:org.springframework.beans.factory.BeanCreationException:   无法自动装配字段:私有   com.test.demo.domain.repository.TbAsmiDragDropRepository   com.test.demo.domain.service.DragDropService.dragDropRepo;嵌套   异常是org.springframework.beans.factory.BeanCreationException:   创建名为&t; tbAsmiDragDropRepository&#39;:Invocation的bean时出错   init方法失败;嵌套异常是   java.lang.IllegalArgumentException:不是托管类型:class   com.test.demo.domain.entity.TbAsmiDragDrop

如果遗漏任何内容或出现问题,请告知

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我们必须设置属性entity-manager-factory-ref =&#34; entitymanagerfactoryrefname&#34;在

<jpa:repositories base-package="com.test.demo.domain.repository" entity-manager-factory-ref="entitymanagerfactoryrefname" />