First of all take a look at below code, I have three main model to manage StoreHouse
and StoreHouseInvetory
's and have other model named StoreHouseInventoryLock
to temporary lock/unlock some amount of StoreHouseInvetory
's when other processes wants to use these models:
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);
}
}
the problem is that when storeHouseInventoryService.sumOfLock
is called in StoreHouseService.syncCurrentInventory
, it does not be aware change of storeHouseInventoryLockService.add
method in PermitService.addDetailToPermitFromStoreHouseInventory
method, and calcuates sum of locks incorrectly.
I think this is because of session was not flushed when I call storeHouseInventoryLockService.add
. If it's true, why hibernate does not flsuh session during this changes? If not, what should I do?