由于缺少关键词来捕捉这种情况,让我继续描述它。课程已经简化。
鉴于这种:
public ItemController {
@Autowired
ItemDtoService ItemDtoService;
@Autowired
DiscountService discountService;
@RequestMapping(value = "/viewItems", method = RequestMethod.POST)
public void process() {
List<ItemDto> ItemDtos = ItemDtoService.getItemDtos();
for(ItemDto i: ItemDtos) {
boolean isDiscounted = discountService.hasDiscount(i); //throws exception here on iteration 2 and the last iteration, ItemDto was discounted
if (isDiscounted) {
i.setPrice(discountService.getDiscountedPrice(i));
//do some other i.setter, basically modify the pojo
}
}
}
}
在以下情况下,discountService.hasDiscount会抛出异常:
例外情况是:
Caused by: org.hibernate.exception.SQLGrammarException: could not update: [somepackage.ItemDto#364]
在堆栈跟踪的某个地方你会看到:
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:456)"
问题是方法调用使用了@Transactional下面的dao方法(也许是有充分理由的,即使它只是一个查询,复杂的查询)。当JPA Tx管理器在方法调用结束时完成其工作时,它会将pojo视为已修改并尝试同步它。 ItemDto pojo确实有@Entity,因为在ItemDtoService.getItemDtos中使用getEntityManager()。createNativeQuery(nativeSql,ItemDto.class)。其他5个课程详情如下:
@Entity
public class ItemDto{
//body
}
@Service
public class ItemService {
@Autowired
ItemDao itemDao;
public List<ItemDto> getItems() {
return itemDao.getItems(); //for sake of simplicity
}
}
@Repository
@Transactional
public class ItemDaoImpl {
public List<ItemDto> getItems() {
String nativeSql = "select...."
return getEntityManager().createNativeQuery(nativeSql, ItemDto.class);
}
}
@Service
public class DiscountService {
@Autowired
DiscountDao discountDao;
public boolean hasDiscount(ItemDto i) {
boolean hasDiscount = discountDao.hasDiscount(i);
//do other service stuff that might influence the hasDiscount flag
return hasDiscount;
}
}
@Repository
@Transactional
public class DiscountDaoImpl {
public boolean hasDiscount(ItemDto i) {
String nativeSql = "select...."
boolean hasDiscount;
//in reality the query is a complicated joins, executes and returns if has discount or not
return hasDiscount;
}
}
我究竟做错了什么?
我尝试和工作的一些选项包括:
我正在寻找其他选项,如果您发现编码方式有问题,请发表评论。
我刚发现的另一个选项是在Dao里面返回ItemDto的列表,在返回列表之前,我会执行这个:
getEntityManager().clear();
它工作正常,因为列表仍然是Dto,并且可以预期这些不需要DB同步,同时保留@Transactional以进行必要的一致读取锁定。
这是另一种选择,但最合适的方式是什么?