-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Join, single class, 2 version columns
PostPosted: Tue May 16, 2006 9:09 am 
Newbie

Joined: Wed Apr 26, 2006 5:21 am
Posts: 14
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=?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 16, 2006 11:10 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Hello,

I do not think that this can be in a single class if you want to use version tracking.

You may try
1)using a Map holding price and subsidiary, if you do not want to set up a business class (I would not do it this way)
2)
set up a relation 1:n
3)
use ternary relations -> Hibernate Reference
4)
use a component ( I do not think that this would be beautiful either).

regards
Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 1:53 am 
Newbie

Joined: Wed Apr 26, 2006 5:21 am
Posts: 14
HI!

Thanks for your reply. Do all of your suggestions imply a 2nd class?

Problem is, this is optional for the customer (most do not need it) and each customer will have different columns seperated. If this would be just different mapping files, it would be not so much hassle.

Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 5:11 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
yes.

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.