Hi all,
I have this interesting, yet frustrating case where Hibernate is getting partial stale data.
So I've configured my Hibernate configurations to manipulate 3 classes: A, B, and C. Class A contains/references B, and B contains a set of C, so something like
A -> B -> C1 -> C2 ....
A, B and C are mapped to different tables, and here is a part of B's hibernate mapping configuration that describes its relationship with C: <set name="C_refs" inverse="true" lazy="true" table="C" fetch="select"> <key> <column name="C_id" not-null="true" /> </key> <one-to-many class="C" /> </set> Of course, A and B have other attributes and may refer to other classes, which are mapped to other tables.
The behavior I notice is that, whenever I create an instance of A (and I added in new Cs for A), the save/update through Session.persist(A)/Session.update(A) correctly saves A, B and Cs. So, I do something like: Session session = sessionFactory.openSession(); Transaction transac = session.beginTransaction(); A a = new A(); B b = new B(); C c = new C(); a.setData("A"); a.setB(b); b.setData("B"); b.addC(c); c.setData("C");
session.persist(a); transac.commit(); print(a.getData()); // Print right value print(a.getB().getData()); // print right value print (a.getB().getC().getData()); // print stale value!! session.close();
However, if I try to get data from the object in memory afterwards, I'm getting stale data for ONLY C. Other attributes of A or B are the updated/persisted new values, except for contents of C. I tried a couple of things: - Commit the session's transaction after persist/update: still getting stale data only for C - Change the flush mode to ALWAYS or COMMIT: still the same thing - Close the session after commit, then open a new session, then perform a query to get A again: only this can help me get the new contents of C
So, I'm very confused at what is causing the behavior. Why am I seeing stale data even after a flush and commit? If I set A.setAttrib("x"), then shouldn't A.getAttrib() return "x"?? (no matter whether the data is updated to db or not, since the updated value is in memory?)
Thanks, mckwil
|