Reputation: 1231
I am using spring-framework 4.0.2.RELEASE
and hibernate-3.6.4.Final
.
This spring data jpa
configuration gives me an error:
No bean named 'entityManagerFactory' is defiend.
However I found that if I change bean name emf
to entityManagerFactory
then, there is no problem.
Can somebody explain why the bean reference does not work here?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- http://drypot.com/post/95?p=6 spring + hibernate: with JPA -->
<tx:annotation-driven />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:packagesToScan="${entitymanager.packagesToScan}" >
<property name="dataSource" ref="dataSource" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<!-- validate | update | create | create-drop -->
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<!--prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop-->
<!-- hibernate ehcache
http://stackoverflow.com/questions/5270998/spring-hibernate-ehcache
-->
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
<prop key="hibernate.cache.factory_class">${hibernate.cache.factory_class}</prop>
</props>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
</beans>
Upvotes: 2
Views: 9367
Reputation: 125232
When working with Spring Data JPA it, by default, looks for a bean named entityManagerFactory
. This is the default for both the xml namespace based configuration (<jpa:repositories />
) and for the java bases configuration (@EnableJpaRepositories
).
If you have a bean with another name you will have to make that clear to Spring Data JPA so that it can select the proper EntityManagerFactory
to use.
<task:repositories entity-manager-factory-ref="emf" />
or java config
@EnableJpaRepositories(entityManagerFactoryRef="emf")
Links
Upvotes: 4
Reputation: 1974
Try injecting your EntityManagerFactory specifing your bean's name like this:
@Autowired
@Qualifier(value = "emf")
private EntityManagerFactory entityManagerFactory;
Upvotes: 0