我正在尝试使用JPQL查询,因此我需要使用实体管理器。
我正在使用Spring,所以我在Spring教程后创建了一个EntityManagerFactory:
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/orm.html#orm-jpa
所以我将这个bean添加到我的WEB-INF / applicationContext.xml中,跟随为em工厂添加的bean:
<beans:bean id="hibernateProperties" class="java.util.Properties">
<beans:constructor-arg index="0">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
</beans:props>
</beans:constructor-arg>
<beans:bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="jpaVendorAdapter">
<beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</beans:property>
<beans:property name="jpaProperties" ref="hibernatePropertiesBean"/>
<beans:property name="packagesToScan">
<beans:list>
<beans:value>dao</beans:value>
</beans:list>
</beans:property>
<beans:bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<beans:property name="entityManagerFactory" ref="entityManagerFactory" />
</beans:bean>
将它注入我的DAO Impl:
@Repository("productDAO")
public class ProductDAOImpl extends GenericDaoHibernateImpl<Product, Integer>
implements ProductDAO {
private EntityManagerFactory emf;
@PersistenceUnit
public void setEntityManagerFactory(EntityManagerFactory emf) {
this.emf = emf;
}
尝试运行查询:
@SuppressWarnings("unchecked")
@Override
@Transactional
public List<Product> getProductsList() {
List<Product> result = new ArrayList<Product>();
EntityManager entityManager = emf.createEntityManager();
...
但是发生了异常:没有定义[javax.persistence.EntityManagerFactory]类型的限定bean
这是我的/WEB-INF/applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<context:property-placeholder location="classpath:application.properties" />
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="${jdbc.driverClassName}" />
<beans:property name="url" value="${jdbc.url}" />
<beans:property name="username" value="${jdbc.username}" />
<beans:property name="password" value="${jdbc.password}" />
</beans:bean>
<beans:bean id="productBean" class="controller.ProductBean">
<beans:property name="productService" ref="productService"> </beans:property>
</beans:bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>model.Product</beans:value>
<beans:value>model.ProductDescription</beans:value>
<beans:value>model.Language</beans:value>
<beans:value>model.ProductDescriptionId</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="productDao" class="dao.ProductDaoImpl"/>
<beans:bean id="productService" class="service.ProductServiceImpl">
<beans:property name="productDao" ref="productDao"> </beans:property>
</beans:bean>
<!-- bean post-processor for JPA annotations -->
<beans:bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config />
<context:component-scan base-package="dao">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
</context:component-scan>
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="sessionFactory"/>
</beans:bean>
</beans:beans>
我的问题是什么?