首先看一下下面的代码,当其他进程想要使用这些模型时,我有三个主要模型来管理StoreHouse
和StoreHouseInvetory
's 并且有其他模型被命名StoreHouseInventoryLock
为临时锁定/解锁一些数量的's:StoreHouseInvetory
public class StoreHouse {
private String name;
private Double currentAmount;
}
public class StoreHouseInventory {
private StoreHouse storeHouse;
private Good good;
private Double amount;
}
public class StoreHouseInventoryLock {
private StoreHouseInventory inventory;
private Double amount;
}
@Service
public class PermitService implements IPermitService {
@Autowired
private IStoreHouseInventoryLockService storeHouseInventoryLockService;
@Autowired
private IStoreHouseService storeHouseService;
@Override
@Transactional
public void addDetailToPermitFromStoreHouseInventory(long permitId, long storeHouseId, long inventoryId, double amount) {
// do some business logic here
/* save is simple method
* and uses Session.save(object)
*/
storeHouseInventoryLockService.add(inventoryId, +amount);
// do some business logic here
storeHouseService.syncCurrentInventory(storeHouseId);
}
}
@Service
public class StoreHouseService implements IStoreHouseService {
@Autowired
private IStoreHouseInventoryService storeHouseInventoryService;
@Autowired
private IStoreHouseInventoryLockService storeHouseInventoryLockService;
@Transactional
public void syncCurrentInventory(storeHouseId) {
/* is a simeple method that use query like below
* select sum(e.amount)
* from StoreHouseInventory
* where e.storeHouse.id = :storeHouseId
*/
Double sumOfInventory = storeHouseInventoryService.sumOfInventory(storeHouseId);
/* is a simeple method that use query like below
* select sum(e.amount)
* from StoreHouseInventoryLock
* where e.storeHouseInventory.storeHouse.id = :storeHouseId
*/
Double sumOfLock = storeHouseInventoryService.sumOfLock(storeHouseId);
// load method is a simple method to load object by it's id
// and used from Session.get(String entityName, Serializable id)
StoreHouse storeHouse = this.load(storeHouseId);
storeHouse.setCurrentAmount(sumOfInventory - sumOfLock);
this.save(storeHouse);
}
}
问题是当storeHouseInventoryService.sumOfLock
被调用时StoreHouseService.syncCurrentInventory
,它不知道storeHouseInventoryLockService.add
方法中PermitService.addDetailToPermitFromStoreHouseInventory
方法的变化,并且错误地计算了锁的总和。
我认为这是因为我打电话时没有刷新会话storeHouseInventoryLockService.add
。如果是真的,为什么休眠在此更改期间不刷新会话?如果没有,我该怎么办?