Hi,
I am using Hibernate 2.1 with ORACLE 8. I read about FlushMode in "Hibernate In Action" book (from Manning EAP), and thought I understood it. But when I tried out a code sample, it was not working the way I was expecting it to, and it'd be great if someone here can help me out.
My code is as :
Code:
public void testIfChangesAreReflectedInTheSameTransaction()
throws HibernateException
{
final long id = 110000000037773404L;
Session session1 = getSessionFactory().openSession(); // open a session
session1.setFlushMode(FlushMode.AUTO); // force changes to be reflected in the same session
Transaction t1 = session1.beginTransaction(); // being transaction
Loan l1 = (Loan) session1.load(Loan.class, new Long(id));
String oldValue = l1.getFicoOverrideBy();
String newValue = Long.toString(System.currentTimeMillis());
l1.setFicoOverrideBy(newValue); // making a change in the session
System.out.println("[1] Changed Value to " + newValue + " from " + oldValue);
Loan l2 = (Loan) session1.createQuery("from Loan l where l.id=" + id)
.uniqueResult(); // running another query to load the same entity
System.out.println("[1] Value read from DB is " + l2.getFicoOverrideBy()); // are changes flushed to DB? Yes
t1.commit(); // commit transaction
session1.close(); // close session
Session session2 = getSessionFactory().openSession(); // open another session
session2.setFlushMode(FlushMode.COMMIT); // force changes NOT to be reflected in the same session
Transaction t2 = session2.beginTransaction(); // being transaction
Loan l3 = (Loan) session2.load(Loan.class, new Long(id));
oldValue = l3.getFicoOverrideBy();
newValue = Long.toString(System.currentTimeMillis());
l3.setFicoOverrideBy(newValue); // making a change in the session
System.out.println("[2] Changed Value to " + newValue + " from " + oldValue);
Loan l4 = (Loan) session2.createQuery("from Loan l where l.id=" + id)
.uniqueResult(); // running another query to load the same entity
System.out.println("[2] Value read from DB is " + l4.getFicoOverrideBy()); // are changes flushed to DB? Yes
t2.commit(); // commit transaction
session2.close(); // close session
}
In the first Session, I want (uncomitted) changes made in a transaction to be visible to queries made within that same transaction. In the second Session, I want (uncomitted) changes made in a transaction
not to be visible to queries made within that same transaction.
The SQL statements fired are :
Code:
Hibernate: select loan0_.LOAN_ID as LOAN_ID0_, [..] loan0_.WEBSITE_ID as WEBSITE_ID0_ from LOAN loan0_ where loan0_.LOAN_ID=?
Hibernate: update LOAN set VERSION_NUMBER=?, [..] WEBSITE_ID=? where LOAN_ID=? and VERSION_NUMBER=?
Hibernate: select loan0_.LOAN_ID as LOAN_ID, [..] loan0_.WEBSITE_ID as WEBSITE_ID from LOAN loan0_ where (loan0_.LOAN_ID=110000000037773404 )
Hibernate: select loan0_.LOAN_ID as LOAN_ID0_, [..] loan0_.WEBSITE_ID as WEBSITE_ID0_ from LOAN loan0_ where loan0_.LOAN_ID=?
Hibernate: select loan0_.LOAN_ID as LOAN_ID, [..] loan0_.WEBSITE_ID as WEBSITE_ID from LOAN loan0_ where (loan0_.LOAN_ID=110000000037773404 )
Hibernate: update LOAN set VERSION_NUMBER=?, PRIMARY_BORROWER_ID=?,[..] WEBSITE_ID=? where LOAN_ID=? and VERSION_NUMBER=?
The order of the SQL statement suggests that I'd get what I want to. But the output of the program is :
Code:
[1] Changed Value to 1090492584966 from 1090492565201
[1] Value read from DB is 1090492584966
[2] Changed Value to 1090492585076 from 1090492584966
[2] Value read from DB is 1090492585076
The first Session works fine - changes made in a transaction are visible to queries fired from that transaction. But the second Session is not working for me, where I would want changes made in a transaction
not to be visible to queries fired in that transaction. Please advise me how to accomplish this effect.
Thanks,
Binil