Spring roo无法初始化代理 - 没有Session

时间:2013-05-27 23:25:51

标签: hibernate transactions spring-roo

我正在使用Spring roo管理一个项目,我正在使用hibernate,当我尝试使用这个控制器方法时,我有这个异常消息:无法初始化代理 - 没有会话

org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:545)
org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:124)
org.hibernate.collection.internal.PersistentSet.iterator(PersistentSet.java:180)
com.macrosystem.rentacar.service.DefaultJournalService.getReservationsProfitPerYear(DefaultJournalService.java:142)
com.macrosystem.rentacar.service.DefaultJournalService.getTotalLossAndProfitPerYear(DefaultJournalService.java:159)
com.macrosystem.rentacar.service.DefaultJournalService.getTotalLossAndProfit(DefaultJournalService.java:173)
com.macrosystem.rentacar.web.JournalController.lossAndPorfit(JournalController.java:44)

这是控制器方法:

@RequestMapping("/lossandprofit.json")
public @ResponseBody List<Map<String, Number>> lossAndPorfit(){
  return journalService.getTotalLossAndProfit() ;
}

以及导致异常的服务方法

@Override
@Transactional
public BigDecimal getReservationsProfitPerYear(int year) {
    BigDecimal reservationsprofit = new BigDecimal(0) ;
    if(vehicle == null){
        log.warn("vehicule is null") ;
    }
    Set<Reservation> reservations = vehicle.getReservations() ;
    Iterator<Reservation> iterator = reservations.iterator() ;
    while(iterator.hasNext()){
        Reservation current = iterator.next() ;
        GregorianCalendar calendar = new GregorianCalendar() ;
        calendar.setTime(current.getStartDate()) ;
        if (calendar.get(Calendar.YEAR) == year){
            reservationsprofit = reservationsprofit.add(current.getAmount()) ;
        }
    }
    return reservationsprofit;
}

为什么即使我使用@Transactionnal注释我的服务方法也有异常,我查看了applicationContext.xml文件并找到了这一行

 <tx:annotation-driven mode="aspectj"
    transaction-manager="transactionManager" />

是取消注释效果的mode =“aspectj”部分吗?

1 个答案:

答案 0 :(得分:1)

得到了同样的错误,在阅读了这个stackoverflow response后,我将以下内容添加到我的web.xml中,解决了这个问题:

<filter>
    <filter-name>jpaOpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>jpaOpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>