Assume this snippet of Hiberante code:
session.beginTransaction();
Event event1 = session.load(1);
Event event2 = new Event(2);
event2.setNextEvent(event1);
event2.save();
session.getTransaction().commit();
My question is, does this work as expected? That is, can I read something from the database, set it on another entity, and then save that entity to the database in a single transaction?
In other words, does beginning the transaction mean “I’ll collect all queries to the database, and hold on to them until you tell me to commit them.” orr does begin really just mean lock and commit means unlock?
I’ve read through your example several times and I cannot see what you are talking about. There are no changes or commits in the code and no indication of whats pending commit and whats not. If this code was was in a typical system making use of an ORM such as hibernate there would be no issue with it that I can see.
Presuming that
ahas not been committed to the database, thendao.findBar(a)would still return it because it would be in hibernates caches. Presuming it has been associated with hibernates session in the first place.dao.findBar(a)should returnx, providing it fits the same criteria asa. i.e. that it already exists or has been associated with the hibernate session.So therefore
dao.findBar(x.getBaz())should also work presuming the same sort of stuff.As you can see there is a lot of “presumably’s” in the above statements. I doubt you will get a better answer without a clearer question.
A further complication is that your question doesn’t mention ORM’s, but you talk about them in the comments. Hence my comment assume that situation. If you are not using an ORM and your dao accesses a database directly through JDBC then the answers would be entirely different again – depending on the internal coding of the dao.