JPA Spring Hibernate Rest API错误会话已关闭

时间:2016-03-22 14:11:34

标签: spring hibernate jpa jax-rs

我使用Spring 4 / Hibernate4作为JAX-RS api。当我调用get methode时,我遇到 org.hibernate.SessionException:会话已关闭  注意:我已经尝试使用@Transactional注释服务和dao方法,但它不起作用

我的上下文看起来像这样

<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();

}

我的服务看起来像这样

    @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;
}

最终我的资源

    @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;
}

1 个答案:

答案 0 :(得分:0)

通过向我的应用程序上下文添加注释bean解决了这个问题

    <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 {