gpt4 book ai didi

hibernate - 多模块项目 spring 3.0 AnnotationSessionFactoryBean packagesToScan 属性值按模块设置

转载 作者:行者123 更新时间:2023-12-04 06:04:21 25 4
gpt4 key购买 nike

我有以下结构:

通用模块

  • 包含常用模块相关、服务和持久化api
  • 包含一个公共(public)持久性上下文 test-common-persistence-context.xml .

  • 搜索模块(取决于通用模块)
  • 包含搜索模块相关的模型bean(标有JPA Entity 注解)、服务和持久化api
  • 包含搜索模块相关的 spring 上下文文件

  • 预订模块(取决于通用模块)
    - 包含预订模块相关的模型bean(标有JPA实体注释)、服务和持久化api
    - 包含预订模块相关的 Spring 上下文文件

    在公共(public)模块中,我有 test-common-persistence-context.xml。对于 sessionFactory bean 有类型
    org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 我需要将属性“packagesToScan”值设置为存在 JPA Entity annotation 标记模型 beans 的包。没有它,我得到异常 Unknown entity: MY_ENTITY_NAME

    通用模块: 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>

    在我的公共(public)模块中没有 JPA 实体,所以我没有要扫描的包,所以我将“packagesToScan”属性值保留为空

    但是,我需要在我的搜索模块和预订模块测试( SearchPersistenceTestBase.java )中使用相同的持久性上下文,以便检测到相应模块中的 JPA 实体。

    搜索模块: 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 {

    }

    有人可以指导我如何使用上面显示的设置来实现这种期望的行为吗?

    **我尝试过的方法**

    我想使用一个额外的 java.lang.String 类型 bean,它的值是从属性文件中设置的
     <bean id="entityPackagesToScan" class="java.lang.String">
    <constructor-arg value="${test.packages.to.scan.jpa.annotations}" />
    </bean>

    其中 test.packages.to.scan.jpa.annotations 在 中定义为空测试通用持久性捆绑包属性

    然后我覆盖 中的 bean 定义测试搜索 Spring 上下文.xml

    测试搜索-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>

    其中 test.packages.to.scan.jpa.annotations 在 中定义为“com.search.model”测试-搜索-bundle.properties

    但这没有用,我得到了异常 Unknown entity: MY_ENTITY_NAME

    谢谢,
    吉涅什

    最佳答案

    好的,我解决了这个问题。 org.springframework.beans.factory.config.PropertyOverrideConfigurer 是需要的。我在这里发布解决方案以供引用,以防有人遇到我在第一篇文章中提到的类似问题:

    通用模块: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>

    搜索模块: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>

    搜索模块: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

    谢谢,
    吉涅什

    关于hibernate - 多模块项目 spring 3.0 AnnotationSessionFactoryBean packagesToScan 属性值按模块设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8536191/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com