Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Hibernate: one-to-one Different Composite Keys

 
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....>
 
Not so fast naughty spawn! I want you to know about
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic