我正在使用 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" 部分吗?