即使我不想保存POJO DTO的JPA同步/提交错误

问题描述 投票:0回答:1

由于缺少关键词来捕捉这种情况,让我继续描述它。课程已经简化。

鉴于这种:

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会抛出异常:

  1. 在后续迭代中
  2. 在上一次迭代中,ItemDto被打折。

例外情况是:

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

}

我究竟做错了什么?

我尝试和工作的一些选项包括:

  1. 在Dao方法上添加@Transactional(readonly = true),因为它们只是查询(但是由于复杂的查询,可能是故意交易的负面影响,并且可能需要锁定以防止脏读)
  2. 在Controller中,创建一个单独的循环进行修改,然后它有2个循环,1个用于循环遍历项目并查看哪些是打折的,将这些信息存储在某个地方以便稍后在第二个循环中引用,这将修改所述pojos

我正在寻找其他选项,如果您发现编码方式有问题,请发表评论。

java jpa transactions commit transactional
1个回答
0
投票

我刚发现的另一个选项是在Dao里面返回ItemDto的列表,在返回列表之前,我会执行这个:

getEntityManager().clear();

它工作正常,因为列表仍然是Dto,并且可以预期这些不需要DB同步,同时保留@Transactional以进行必要的一致读取锁定。

这是另一种选择,但最合适的方式是什么?

© www.soinside.com 2019 - 2024. All rights reserved.