aspose file tools*
The moose likes Object Relational Mapping and the fly likes Dirty Checking not Achieving Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Dirty Checking not Achieving " Watch "Dirty Checking not Achieving " New topic
Author

Dirty Checking not Achieving

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Hi,

I am using this code for hibernate but as per my knowledge 'Dirty Checking' has to work !

bit it is not working ! why so ?

----------------------------
User user = (User)session.get(User.class,1);

user.setName("raman");

//session.save(user);
tx.commit();

---------------------------

I don't any data updated in the user table.

mysql> select * from user;
Empty set (0.00 sec)

What is cause to not acieving Dirty Checking here ?

is i have to configure any where in the hbm file ?
Regards,
prabhat
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Nothing to do with dirty checking - your call to session.save() is commented out.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
I have marked it commented to check whether Dirty Checking is working or not !


See the below links :


http://www.hibernate-training-guide.com/dirty-checking.html

http://whitesboard.blogspot.com/2009/10/hibernates-smart-automatic-dirty-check.html


why will call the save method with session Object. It is the Hibernate who take care of all this before committing the Transaction.
Victor Dolirio
Ranch Hand

Joined: Aug 21, 2008
Posts: 57
Can you post the whole unit of work code?


If happen something bad in you life, don't ask God why it happened. Ask for what.
SCJP 5 (80%) - SCWCD 5 (91%) - SCBCD & SCJD coming soon...
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Now i got the solution for this :

I have to call like this then it will work .

Configuration cfg=new Configuration();
sessionFactory sessionFactory=cfg.configure("hibernate.cfg.xml").buildSessionFactory();
Session session = null;


user.setName("Prabhat");
session.save(user);

user.setName("Raman"); // It is actually Hibernate Dirty Checking now , here i am not calling the session.save(user);

/* but still here hibernate marks that this object has been modifed and will save while committing the Transaction.


tx.commit();

session.flush();
session.close();

I think the above example is very clear understanding of the Dirty Checking.


but if you see few links in net where it is mentioned somehow wrongly... so i was confused.

http://www.hibernate-training-guide.com/dirty-checking.html

http://whitesboard.blogspot.com/2009/10/hibernates...art-automatic-dirty-check.html


please confirm and let me know if any one agree over here on my views or correct me if i am wrong !

Regards,
prabhat
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Of those two links, the one that work is wrong. I'd stick to the Hibernate documentation if you need examples.
Victor Dolirio
Ranch Hand

Joined: Aug 21, 2008
Posts: 57
I'm sure that with this code you will get a NPE. But regardless of this details, dirty checking works fine to anyone object held in the primary cache (persistent context) in the persistent state. So the code above must works to. Is possible to disable dirty checking setting the entity as readonly (there are several ways to do this) and maybe you acidentally enable this feature. Can you post the indeed code you are using and the mapping if possible?

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Hi,

I am not using any mapping conf file.

Just i am using 2 case:

1) user.setName("Prabhat");
session.save(user);

user.setName("Ranjan");

here Dirty Checking is working.
2) user.setName("Raman");

in this case i am not calling Save method any time so i was expecting thet Dirty checking has to work as per the notes in net from few links.

But i was wrong and i corrected the notes and so case 1 is correct way to test Dirty Checking.

if you know how to map or use mapping file to disbale or enable Dirty Checking , please let me know !


Thanks in Advance

Prabhat

Victor Dolirio
Ranch Hand

Joined: Aug 21, 2008
Posts: 57
If you already have the object user with id equals to 2L save in the database, this code will trigger dirty checking normally:



If the object not exists, you must save before. Maybe this is your problem.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
ok will try it again and test run , then i can confirm you !

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Hi

I have checked

i am using the belwo statement:

Case-1
---------------------Dirty Checking Not Working -------------------

Session session =sessionFactory.openSession();

Transaction tx = session.beginTransaction();

User user = (User)session.get(User.class,1);
user.setName("Raman");

tx.commit();

Data for table user is present here but not updated the records with UserId =1

Case-2:

-------------------Dirty Checking working here --------- As i alreday mentioned in last post ----

User user = new User();
user.setName("Prabhat");
session.save(user);

user.setName("Raman"); //Dirty Checking Working

// here not calling save method again

tx.commit();

please correct me if i am wrong nay way ! or you can also perform the same test.

Prabhat
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dirty Checking not Achieving