【发布时间】:2015-08-23 15:20:16
【问题描述】:
假设我在表“X”上有这些数据:
ID UpdatedDate ParentID
001 2015-01-01 1
002 1
003 1
代码是(我简化了代码):
...
@Autowired
private XDao xDao;
private void createTransaction(ParentData parentData) {
List<XData> xDataList = xDao.getDataByParentIdAndUpdatedDateIsNull(parentData.getId());
XData xData = null;
// Then we get only the first row of the list
if(xDataList != null && xDataList.size() > 0) {
xData = xDataList.get(0);
} else {
return;
}
// Another transaction
...
// Then we update the UpdatedDate
xData.setUpdatedDate(new Date());
xDao.saveAndFlush(xData);
// And we call the createTransaction again until there is no more xData with a null UpdatedDate on a Parent ID
createTransaction(parentData);
}
但是我得到的是一个永无止境的过程,当我检查数据库时,它总是在同一个父 ID 上相互影响数据。所以数据库是这样的:
第一次迭代:
ID UpdatedDate ParentID
001 2015-01-01 1
002 2015-02-02 1
003 1
第二次:
ID UpdatedDate ParentID
001 2015-01-01 1
002 1
003 2015-02-02 1
第三个:
ID UpdatedDate ParentID
001 2015-01-01 1
002 2015-02-02 1
003 1
等等。怎么了?
这是 getDataByParentIdAndUpdatedDateIsNull 类(我简化了代码):
...
public static final String GET_DATA_BY_PARENTIDANDUPDATEDDATEISNULL =
"SELECT o FROM XData o " +
"WHERE o.parentData.parentId = ?1 " +
" AND o.updatedDate IS NULL";
@Query(GET_DATA_BY_PARENTIDANDUPDATEDDATEISNULL)
public List<XData> getDataByParentIdAndUpdatedDateIsNull(Long parentId);
【问题讨论】:
-
你能提供
xDao.getDataByParentIdAndUpdatedDateIsNull的代码吗? -
@mrjimoy_05,你能分享你在这个问题上的发现吗?
标签: java jpa spring-data-jpa overlap transactional