2016-03-22 64 views
0

我正在使用Spring 4/Hibernate4作为JAX-RS api。当我打电话让我梅索德碰上org.hibernate.SessionException:会话关闭 注:我已经尝试过用注释@Transactional的服务和DAO梅索德,但它不工作JPA Spring Hibernate Rest API错误Session关闭

我背景是这样的

<bean id="ppEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="persistenceXmlLocation" value="classpath:config/persistence-demo.xml" /> 
    <property name="persistenceUnitName" value="ppRestPersistenceLegacy" />   
    <property name="dataSource" ref="ppRestLegacyDS" /> 
    <property name="packagesToScan" value="fr.test.rest*" /> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
      <property name="showSql" value="true" /> 
      <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" /> 
     </bean> 
    </property> 
</bean> 
<bean id="ppRestLegacyDS" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton"> 
    <property name="jndiName" value="java:comp/env/jdbc/ppRestLegacyDB" /> 
    <property name="resourceRef" value="true" />   
</bean> 

在我的DAO我有

@PersistenceContext(unitName = "ppRestPersistenceLegacy") 
private EntityManager entityManager; 

public List<RcPosStatus> getRcPosStatus(String orderByInsertionDate) { 

    String sqlString = null; 
    if(orderByInsertionDate != null){ 
     sqlString = "SELECT s FROM RcPosStatus s" + " WHERE ROWNUM < 11 ORDER BY s.DATE_FROM " + orderByInsertionDate; 
    } else { 
     sqlString = "SELECT s FROM RcPosStatus s WHERE ROWNUM < 11"; 
    }   
    TypedQuery<RcPosStatus> query = entityManager.createQuery(sqlString, RcPosStatus.class);   
    return query.getResultList(); 

} 

我SERVICE L本这样

@Autowired 
BufferDao bufferDao; 


// ******************** Read related methods implementation **********************  
public List<Buffer> getBuffers(String orderByInsertionDate, Integer numberDaysToLookBack) throws AppException { 

    if(isOrderByInsertionDateParameterValid(orderByInsertionDate)){ 
     throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(), 400, "Please set either ASC or DESC for the orderByInsertionDate parameter", null , AppConstants.BLOG_POST_URL); 
    }   
    List<Buffer> buffers = bufferDao.getBuffers(orderByInsertionDate); 

    return buffers; 
} 

和finaly我的资源

@GET 
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) 
public List<RcPosStatus> getRcPosStatus(
     @QueryParam("orderByInsertionDate") String orderByInsertionDate, 
     @QueryParam("numberDaysToLookBack") Integer numberDaysToLookBack) 
     throws IOException, AppException { 
      List<RcPosStatus> status = statusService.getRcPosStatus(
      orderByInsertionDate, numberDaysToLookBack); 
    return status; 
} 

回答

0

ooks通过添加注释豆到我的应用程序上下文

<tx:annotation-driven transaction-manager="transactionManager" /> 
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="ppEntityManagerFactory" /> 
</bean> 

和注释的服务与梅索德@Transactional

解决问题
 // ******************** Read related methods implementation **********************  
@Transactional 
public List<RcPosStatus> getRcPosStatus(String orderByInsertionDate, Integer numberDaysToLookBack) throws AppException { 
相关问题