繁体   English   中英

将JPA的SessionFactory配置成Spring

[英]Configure SessionFactory of JPA into Spring

我正在尝试将 Jar 添加到我的项目中,该项目使用 Spring 4 sessionFactory 连接到数据库。 我的项目使用 Spring 引导和 JPA 进行交易。

如何配置我的项目以便将 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。

My projects has spring-boot-starter-data-jpa:2.2.4.RELEASE, and the jar uses Spring 4. Currently I am not able to edit the second jar (the one with 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}")

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM