休眠-如何弹簧注入EntityManager?

前端之家收集整理的这篇文章主要介绍了休眠-如何弹簧注入EntityManager? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我遵循指南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"
           class="com.group.DecryptingPropertyPlaceholderConfigurer"
           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" class="com.package.service.OrderDaoImpl"/>

     <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerfactorybean">
         <property name="persistenceUnitName" value="MyServer"/>
         <property name="loadTimeWeaver">
             <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
         </property>
         <property name="dataSource" ref="dataSource"/>
         <property name="jpaVendorAdapter">
             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                 <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" class="org.springframework.orm.jpa.JpaTransactionManager">
         <property name="entityManagerFactory" ref="entityManagerFactory"/>
         <property name="dataSource" ref="dataSource"/>
     </bean>

     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         <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" class="java.util.concurrent.Executors" 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注释中?
原文链接:https://www.f2er.com/spring/531657.html

猜你在找的Spring相关文章