Kramer
Kramer

Reputation: 1058

Can I use spring-data-jpa with hibernate?

I have a project that uses Spring 4.3.2 and Hibernate 4.3.10.

I would like to add a spring data JpaRepository.

As soon as I add the dependency spring-data-jpa:jar:1.8.0.RELEASE, I get the following exception:

java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
    at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:170)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1082)
    at org.springframework.beans.factory.access.SingletonBeanFactoryLocator.useBeanFactory(SingletonBeanFactoryLocator.java:396)

on the line:

BeanFactory bf = locator.useBeanFactory(beanFactoryName)

I.e. I'm not actually using any spring-data classes. I'm just adding the jar.

Relevant dependencies are these:

[INFO] |  +- org.hibernate:hibernate-entitymanager:jar:4.3.10.Final:compile
[INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] |  |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  |  \- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile
[INFO] +- org.springframework:spring-core:jar:4.3.2.RELEASE:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.2:compile
[INFO] +- org.springframework:spring-beans:jar:4.3.2.RELEASE:compile
[INFO] +- org.springframework:spring-context:jar:4.3.2.RELEASE:compile
[INFO] |  \- org.springframework:spring-aop:jar:4.3.2.RELEASE:compile
[INFO] +- org.springframework:spring-context-support:jar:4.3.2.RELEASE:compile
[INFO] +- org.springframework:spring-tx:jar:4.3.2.RELEASE:compile
[INFO] +- org.springframework:spring-jdbc:jar:4.3.2.RELEASE:compile
[INFO] +- org.springframework:spring-orm:jar:4.3.2.RELEASE:compile
[INFO] +- org.springframework.webflow:spring-binding:jar:2.4.0.RELEASE:compile
[INFO] |  \- opensymphony:ognl:jar:2.6.11:compile
[INFO] +- org.springframework:spring-expression:jar:4.3.2.RELEASE:compile
[INFO] +- org.hibernate:hibernate-ehcache:jar:4.3.10.Final:compile
[INFO] |  \- org.hibernate:hibernate-core:jar:4.3.10.Final:compile
[INFO] +- net.sf.ehcache:ehcache:jar:2.10.4:compile
[INFO] +- org.aspectj:aspectjrt:jar:1.8.5:compile

It seems like there must be some sort of incompatibility, but I can't fathom what. Any help is appreciated!

Upvotes: 0

Views: 119

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81907

Can I use Spring Data JPA with Hibernate?

Absolutely! Spring Data JPA is built on JPA and Hibernate is the most popular JPA implementation.

Regarding the exception you are getting:

I'd start by moving to more recent versions of Spring, Spring Data and Hibernate. The versions you mention are really old.

That's not likely to fix the exception though, for this I recommend to

  1. search for the exception, maybe plus "Spring Data" check if any of the results match your situation.

  2. Find out what is initializing the ApplicationContext because it seems to happen twice. Once through the line, you quoted. Unfortunately, you didn't mention where that line is. One way to do that is to put a breakpoint at the check that triggers the exception. Your code should get there twice. Once when it triggers the exception and once before that. Then get rid of one of the executions.

Upvotes: 1

Related Questions