Search code examples
javaspringspring-bootjpasessionfactory

Configure SessionFactory of JPA into Spring


I am trying to add a Jar to my project that uses Spring 4 sessionFactory to connect to DB. My Project uses Spring boot with JPA for transactions.

How do I configure my project in order that the JPA sessionFactory is passed to the Spring sessionFactory? .

When I put the jar in my project, I got the error: "Consider defining a bean named 'sessionFactory' in your configuration."

So, I add a sessionFactory on my applicationContext.xml:

<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>

But, when I add this, the following error appears:

Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

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). Is this configuration possible? Thanks!

First project config: (Base project)

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')

Second project config: (Jar that I need to add)

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

Solution

  • I solved this by keeping only SpringBoot + JPA Repositories, everything in the same version.