【问题标题】:Configure SessionFactory of JPA into Spring将 JPA 的 SessionFactory 配置到 Spring 中
【发布时间】:2020-10-31 21:21:44
【问题描述】:

我正在尝试向我的项目添加一个使用 Spring 4 sessionFactory 连接到数据库的 Jar。我的项目使用带有 JPA 的 Spring Boot 进行事务处理。

如何配置我的项目以便将 JPA sessionFactory 传递给 Spring sessionFactory? .

当我将 jar 放入我的项目时,我收到错误:“考虑在您的配置中定义一个名为 'sessionFactory' 的 bean。”

所以,我在 applicationContext.xml 上添加了一个 sessionFactory:

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

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan">
            <list>
                <value>com.secondProjectClass</value>
            </list>
        </property>
    </bean>

但是,当我添加这个时,会出现以下错误:

行动:

考虑在您的配置中定义一个名为“entityManagerFactory”的 bean。

我的项目有 spring-boot-starter-data-jpa:2.2.4.RELEASE,jar 使用 Spring 4。 目前我无法编辑第二个 jar(带有 spring 4 的那个)。 这种配置可行吗?谢谢!

第一个项目配置:(基础项目)

implementation ('org.springframework.boot:spring-boot-starter-data-jdbc')
implementation ('org.springframework.boot:spring-boot-starter-web')
compile ('org.springframework.boot:spring-boot-starter-data-jpa:2.2.4.RELEASE')

第二个项目配置:(我需要添加的Jar)

springVersion = '4.3.13.RELEASE'
compile("org.springframework:spring-context:${springVersion}")
compile("org.springframework:spring-web:${springVersion}")
compile("org.springframework:spring-tx:${springVersion}")

【问题讨论】:

    标签: java spring spring-boot jpa sessionfactory


    【解决方案1】:

    我通过只保留 SpringBoot + JPA 存储库来解决这个问题,所有东西都在同一个版本中。

    【讨论】:

      猜你喜欢
      • 2017-06-01
      • 2015-12-31
      • 1970-01-01
      • 2013-01-26
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多