• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@Transactionnal + Hibernatetemplate Without saving dirty objects

 
saad bousfiha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already search in forums, and i tried many things without succes.

I want at the end on a transactional method, and only at the end, to save only hibernate object for whom i will use explicitly the Hibernate getHibernateTemplate().saveOrUpdate() method, and NOT dirty ones, that i only modify using setters.

My need will be clearer after reading my service layer code:

For simplicity purpose, i will summarize my code.

Service layer:
--------------


So, what i need, is at the end of the method updateCart1():
- to save the change of cartB.
- Not to save the change of cartA.



Dao layer:
----------
* cartDao's methods (findById, saveOrUpdate) call the HibernateTemplate one.



* I didn't want that Dao's method act on database outside a transactionnal service layer method. And i wanted them to be executed in database at the end of a transaction defined on the service layer. Thus, in hibernate spring configuration file


* Note that i am using OpenSessionInViewFilter for Lazyu loading purpose. So in my XML file:


NB: These cart cartDao's methods are working fine in a normal scenario. And "@Transactional" is correctly configured and work fine two. But not in the one i'm describing.

Problem: With this code, i get the two objects (cartA and cartB) changed in database, while i dont want to modify cartA, for whom i didn't call explicitly the HibernateTemplate's saveOrUpdate method.

After searching in forum's, i tried to make the hibernate FLUSH mode to FLUSH_NEVER. But at the execution of CartDao.Update(cartB) i get this exception:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

Please, can you help me?
 
saad bousfiha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



JB Nizet: But you're fighting against Hibernate. I don't find it stylish, but hackish. Even if you manage to do it (and you probably won't), every other developer (including yourself, some weeks or months later) won't understand why something which should be updated given the normal Hibernate behavior is not updated, because of this hack. – JB Nizet 12 secs ago
 
saad bousfiha
Greenhorn
Posts: 3
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic