Hi tetrione.
Thanks for the help, you understood the problem very well. But your solution will not solve the problem.
Code:
session.get(ob.class, ob.getId ());
will hit DB if dbob does not exist. I don't want to hit the DB each time I want to know if a Object is associated with my Session.
the second idea is good, but I don't like to throw exceptions when I'm just trying to check if the object is already in the session. Indeed it is exactly this exception I'm trying to avoid. Exceptions can cause problems. I can use this method, but again, it's not a good solution.
Code:
session.lock (ob, LockMode.NONE);
getCurrentLockMode(ob) will also throw exception, since ob is not yet attached.
What I want to do is:
Code:
if exists object dbobj in the session (but please, don't go to database to check this,nor throw slow exceptions ) with id=x
ob=dbob;
// or copy props from ob to dbob.
else {
ob = new X();
ob.pk=myid;
lock ob;
}
In this particular case I'm worried in db hit becouse ob is just a not null property of another object that was changed. I'm not going to save it, just lock. But another part of my business may had changed ob. Or may not.
Is the book complete ? I got some demo chapters.