Hibernate not reading data from Persistence cache, instead going to DB


#1

I am facing a weird issue. I have a hierarchy like Manager -> Dao. I have update method in Manager which takes the domain object as input and Id of the entity as param. I am doing dao.findById in manager and then converting domain object to entity object passing it to dao. In dao, I am again doing the findById call in Dao (as update can be called directly from DAO also), now this time hibernate again fetching the data from the DB instead of taking it from persistence context. May someone please help me in this case?

In Manager: 
@Override
    @Transactional(rollbackFor = Exception.class)
    public E update(E entry, ID id) throws ManagerException {
        try {
            B bean = getDao().findById(id);
            bean = convertToEntity(entry, bean);
            B newEntity = getDao().update(bean, id);
            E newEntry = convertToEntry(newEntity);
            return newEntry;
        } catch (DaoException de) {
            throw new ManagerException(de);
        }
    }

In DAO:

public T Update(T entity,ID id){
               T oldEntity = findById(id);
		if (oldEntity == null) {
			throw new DaoException(ERPErrorCodes.RECORD_NOT_FOUND);
		}

		return update(entity, oldEntity, id);
}

#2

It’s impossible to tell what you are doing without seeing all the code you are calling from this service method.

For instance, what’s the purpose of converting an actual managed entity to an “entity”?


#3

Thanks for answer Sir.
I am converting managed entity to get the changed values from domain object, which is in this case is entry object. I got your point Sir, that dao.update() call is not required.
But still not able to figure out why second “select call” is going to DB.


#4

It’s impossible to tell what you are doing without seeing all the code you are calling from this service method.