Hi,
I have a one-to-many relationship in survey mapping file is defined as:
Code:
<set name="questions" inverse="true" order-by="SEQUENCE_NO">
<key>
<column name="SURVEY_ID" not-null="true" />
</key>
<one-to-many class="XXX.Question">
</set>
A survey consists of one or many questions. The order of the question can be changed by modifying SEQUENCE_NO field in question table.
The problem is whenever I changed the sequence_no, the questions set in survey object doesn't reorder to the correct sequence. Here is the rough flow of the action:
1. Open-session-in-view servlet filter opens a session and a read-only transaction T1
2. Another read-write transaction T2 is created -> get survey instance -> initialize the set of questions in the survey instance (coz of lazy="true") -> change the sequence_no in one of the questions -> commit the transaction
3. [Resume T1 transaction] My view get the survey instance (which has been instantiated in step 2, so it is from first-level cache). When I tried to display out the questions, the order doesn't updated to reflect the change in step 2.
Can anyone advise me on how to solve this? I got two solutions, but I don't really like the way of doing it:
1. Do a session.refresh(surveyObj) after committed the transaction in step 2 to load the list from database again. I cannot refresh it before the transaction is commited, coz I'm relying on hibernate transaction dirty-check. But, the problem is I have to call refresh() manually.
2. Don't use bi-directional relationship, throw away the one-to-many relationship. Use HQL query to get the list of questions instead.
Anyone got a better solution? Thanks!!
Merry christmas to all!