我遵循指南here,但是当DAO执行时,EntityManager为null.
我尝试了在指南的注释,各种论坛以及此处(包括this)中找到的许多修复程序,但均无济于事.无论我似乎做什么,EntityManager都保持为空.
以下是相关文件,其中软件包等已更改,以保护无辜者.
spring-context.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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" xmlns:p="http://www.springframework.org/schema/p"> <context:component-scan base-package="com.group.server"/> <context:annotation-config/> <tx:annotation-driven/> <bean ID="propertyPlaceholderConfigurer" p:systemPropertIEsModename="SYstem_PROPERTIES_MODE_OVERRIDE"> <property name="locations"> <List> <value>classpath*:spring-*.propertIEs</value> <value>classpath*:${application.environment}.propertIEs</value> </List> </property> </bean> <bean ID="orderDao" /> <bean /> <bean ID="entityManagerFactory" > <property name="persistenceUnitname" value="MyServer"/> <property name="loadTimeweaver"> <bean /> </property> <property name="dataSource" ref="dataSource"/> <property name="jpavendorAdapter"> <bean > <property name="showsql" value="${com.group.server.vendoradapter.showsql}"/> <property name="generateDdl" value="${com.group.server.vendoradapter.generateDdl}"/> <property name="database" value="${com.group.server.vendoradapter.database}"/> </bean> </property> </bean> <bean ID="transactionManager" > <property name="entityManagerFactory" ref="entityManagerFactory"/> <property name="dataSource" ref="dataSource"/> </bean> <bean ID="dataSource" > <property name="driverClassname" value="${com.group.server.datasource.driverClassname}"/> <property name="url" value="${com.group.server.datasource.url}"/> <property name="username" value="${com.group.server.datasource.username}"/> <property name="password" value="${com.group.server.datasource.password}"/> </bean> <bean ID="executorService" factory-method="newCachedThreadPool"/> </beans>
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> <persistence-unit name="MyServer" transaction-type="RESOURCE_LOCAL"/></persistence>
OrderDaoImpl
package com.group.service; import com.group.model.Order; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.query; import java.util.List; @Repository @Transactional public class OrderDaoImpl implements OrderDao { private EntityManager entityManager; @PersistenceContext public voID setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; } @OverrIDe public Order find(Integer ID) { Order order = entityManager.find(Order.class,ID); return order; } @OverrIDe public List<Order> findAll() { query query = entityManager.createquery("select o from Order o"); return query.getResultList(); } @OverrIDe public List<Order> findBySymbol(String symbol) { query query = entityManager.createquery("select o from Order o where o.symbol = :symbol"); return query.setParameter("symbol",symbol).getResultList(); } }
最佳答案您是否尝试过将unitname =“ MyServer”添加到@PersistenceContext注释中? 总结 以上是内存溢出为你收集整理的休眠-如何d簧注入EntityManager? 全部内容,希望文章能够帮你解决休眠-如何d簧注入EntityManager? 所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)