Olá pessoal, estou tentando utilizar o Spring com o JPA, porém não estou conseguindo fazer a injeção de dependencia do entityManagerFactory, estou urilizando uma aplicação web, será que alguem pode me ajudar?
jps-servlet.xml<?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: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.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean name="/hello.htm" class="orm.controller.Teste" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>
<servlet>
<servlet-name>jpa</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jpa</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/jpa-servlet.xml
</param-value>
</context-param>
@Entity
public class Product {
private int id;
private String name;
public Product() {
}
@SequenceGenerator(name = "generator", sequenceName = "seq_product", allocationSize = 1)
@Id
@GeneratedValue(strategy = SEQUENCE, generator = "generator")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@PersistenceContext
public class ProductDAO {
@PersistenceUnit
private EntityManagerFactory _entityManagerFactory = null;
private EntityManager _entityManager = null;
public ProductDAO() {
_entityManager = _entityManagerFactory.createEntityManager();
}
public void insert(Product product) {
_entityManager.getTransaction().begin();
_entityManager.persist(product);
_entityManager.getTransaction().commit();
}
public void delete(Product product) {
_entityManager.getTransaction().begin();
_entityManager.remove(product);
_entityManager.getTransaction().commit();
}
public void update(Product product) {
_entityManager.getTransaction().begin();
_entityManager.merge(product);
_entityManager.getTransaction().commit();
}
public Product findByPK(Object obj) {
return _entityManager.find(Product.class, obj);
}
public ArrayList<Product> findAll() {
Query query = _entityManager.createQuery("from Product" );
return (ArrayList<Product>) query.getResultList();
}
public EntityManagerFactory get_entityManagerFactory() {
return _entityManagerFactory;
}
public void set_entityManagerFactory(EntityManagerFactory managerFactory) {
_entityManagerFactory = managerFactory;
}
public EntityManager get_entityManager() {
return _entityManager;
}
public void set_entityManager(EntityManager manager) {
_entityManager = manager;
}
}
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="maindatabase" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.connection.username" value="postgres" />
<property name="hibernate.connection.password" value="database" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/exemplo" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
public class Teste implements Controller{
protected final Log logger = LogFactory.getLog(getClass());
@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
ProductDAO productDAO = new ProductDAO();
Product product = new Product();
product.setName("Teste");
productDAO.insert(product);
String now = (new Date()).toString();
logger.info("Returning hello view with " + now);
return new ModelAndView("hello", "now", now);
}
}

