• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: one-to-one Different Composite Keys

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 classes viz. Product and ProductBasic



Product is mapped to a table PRODUCT which has PRODUCTID as PK.
ProductBasic is mapped to a table PRODUCTBASIC which has PRODUCTID,USETYPE and STARTTTIMESTAMP as composite key. Also PRODUCTID
is the foreign key referencing PRODUCT table...

How can i define my one-to-one mapping considering ProductBasic.productId should get its value from Product.productId
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually Product and ProductBasic have one-to-many relationship at database side, but logically i made it to one-to-one (using endTimeStamp column) as:

PRODUCT:
--------
PRODUCTID PRODUCTNAME
1 Pen
2 Bottle


PRODUCTBASIC
------------
PRODUCTID USETYPE STARTTIMESTAMP ENDTIMESTAMP
1 1 01-01-2009 02-01-2009
1 1 02-01-2009 05-01-2009
1 1 05-01-2009 31-12-9999
2 1 10-01-2009 31-12-9999


ProductId 1 has many rows in ProductBasic but i am only interested in one row whose startTimeStamp<currentTime><ENDTIMESTAMP
(whose endTimestamp = 31-12-9999) only.... Hence while retriving i will always get 1 row....
Actually when one row is inserted in ProductBasic it is inserted (with endTimeStamp always as 31-12-9999) as:
PRODUCTID USETYPE STARTTIMESTAMP ENDTIMESTAMP
3 1 01-01-2009 31-12-9999

Whenever this row is modified, we modify the EndTimeStamp of current row (as endtimeStamp=currentTimeStamp) and insert another row (with
starttimestamp=currentTimestamp and endtimestamp=31-12-9999) as:

PRODUCTID USETYPE STARTTIMESTAMP ENDTIMESTAMP
3 1 01-01-2009 10-01-2009
3 1 10-01-2009 31-12-9999

So every product in ProductBasic table has only one row as active (whose endtimestamp=31-12-9999)....
How can i define this logical one-one-one relationship in Hibernate and retrieve and save proper data....>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic