【问题标题】:Spring roo could not initialize proxy - no SessionSpring roo 无法初始化代理 - 没有会话
【发布时间】:2013-05-22 19:38:06
【问题描述】:

我正在使用 Spring roo 管理一个项目,并且我正在使用休眠,当我尝试使用此控制器方法时,我收到以下异常消息:无法初始化代理 - 无会话

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"部分取消了那个注解的效果吗?

【问题讨论】:

    标签: hibernate transactions spring-roo


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 2020-06-07
      相关资源
      最近更新 更多