Reputation: 4447
My current working code:
EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("TT-SpringMVCPU");
EntityManager em = emf.createEntityManager();
I would like to replace that with something like this:
@PersistenceContext(unitName = "TT-SpringMVCPU")
private EntityManager _entityManager;
When I try that, I get this error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'showController': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'TT-SpringMVCPU' is defined
org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:341)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
...
What did I forget to configure?
Upvotes: 0
Views: 1604
Reputation: 570365
What did I forget to configure?
Well, you didn't show anything :) Anyway, Using JPA in Spring without referencing Spring shows one way to do things:
Configuration
LocalEnityManagerFactoryBean
to create theEntityManagerFactory
JpaTransactionManager
to manager JPA transactions<tx:annotation-driven />
to tell Spring to look for@Transactional
- Your bean definition!
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" /> <bean id="productDaoImpl" class="product.ProductDaoImpl"/> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <bean class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <tx:annotation-driven /> </beans>
That's it. Two annotations and four bean definitions.
Have a look.
Upvotes: 2
Reputation: 139931
From the Spring reference manual: Three options for JPA setup in a Spring environment
Upvotes: 2