How does the setup look like?
I have an object "Something". This has a set of "Items". I'm listing the items of "Something" on a web page.
- Sidenote - The "sum of values" is a formula property. So it's not in the database.
Code:
Sum of values: X
List:
Item 1 - value1
Item 2 - value2
What is the configuration?Something:
Code:
<class name="Something" table="somethings">
<cache usage="read-write" />
<id name="somethingID" column="somethingID">
<generator class="native" />
</id>
<property name="sum"
insert="false"
update="false"
formula="(SELECT COALESCE(SUM(i.value), 0) FROM items i WHERE i.somethingID = somethingID))"
/>
<set name="items" inverse="true" cascade="all-delete-orphan" order-by="somethingID">
<cache usage="read-write" />
<key column="somethingID" on-delete="cascade" />
<one-to-many class="Items" />
</set>
</class>
What's am I trying to do?I'm doing three things:
Code:
1) Show a page with items, together with a sum of it's values (the formula property of "something".
2) I update a value of an Item and click the update button.
3) Perform the update and reload the page with the correct values and sum
This last step needs a little explanation: If I click the "update" button,
1) a transaction is started
2) the values are updated (item.setValue...)
3) then I build the page with Something.getSum() in it
4) ... and I add the Items (each item with item.getValue())
5) I commit the transaction
What is the problem?
The values get updated and are displayed CORRECTLY, but the sum doesn't get updated until I reload the page once more. (and it retrieves the committed values from the database with the sum formula)
What's my own view on this?
The item values get displayed correctly, because I set them with the Setter method. That way, they get updated in memory. Too bad, the transaction hasn't been committed yet, so Something.getSum() still has the old value in memory.
What's the question then?
I wonder if it's possible to tell hibernate to run something's "sum formula" on the items in memory (that are already updated). I can't play with the transaction, else it all gets screwed up and hibernate complains about a closed session etc. So that's not a solution.