HI!
In our application (to be ported to Hibernate), we optionally allow the customer to split a table into a common part and a specific part for each subsidiary.
For example, items may have identical description, but different price for each subsidiary.
So, this looks like some many-to-one mapping, but it has to be in a single class.
Until now, this has been achieved by 2 tables, 1 class, 1 view.
Small example:
Code:
Table ITEMS
id
timestamp
ITEMNUMBER
PRICE
SUBSIDIARY
Table ITEMS_A
id
timestamp
ITEMNUMBER
DESCRIPTION
id and timestamp are being generated automatically by SQL Server.
So, there will be for a single item one row in ITEMS_A and n rows in ITEMS (for each subsidiary).
We created a mapping for table ITEMS and specified to join table ITEMS_A:
Code:
<join table="ITEMS_A">
<key column="ITEMNUMBER"/>
<property name="description"/>
</join>
We also specified to correctly handle the id and timestamp columns for table ITEMS:
Code:
<id name="id" access="field">
<generator class="native"/>
</id>
<version name="timestamp" type="byte[]" generated="always" access="field"/>
Now, simple things work with this mapping. What is missing, is the handling of the timestamp column in table ITEMS_A.
When updating an item, Hibernate issues correctly 1 or 2 update statements, but uses the timestamp column in the where clause only for update to the ITEMS table, not for the ITEMS_A table.
So, if another subsidiary has altered the item at the same time and changed one "common" column in ITEMS_A, this will NOT result in an optimistic locking error, but it should.
We tried using a 2nd <version> specification, but Hibernate won't allow that.
How can we solve this problem? Maybe the mapping with join is not correct at all for this situation, but the other mappings always seem to require 2 classes.
Thanks!
Thomas
Hibernate version:
3.2.0.CR1
Name and version of the database you are using:
SQL Server 2000
The generated SQL (show_sql=true):
update ITEMS set itemnumber=?, price=?, subsidiary=? where id=? and timestamp=?
update ITEMS_A set description=? where ITEMNUMBER=?