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.  [ 3 posts ] 
Author Message
 Post subject: A peculiar mapping problem
PostPosted: Mon Feb 09, 2004 6:04 pm 
Beginner
Beginner

Joined: Fri Oct 24, 2003 4:28 pm
Posts: 20
Hi all,

I have a domain object that is versioned (the current version is in one table and then each time it's changed, the last version is archived to a seperate table with the same structure). There are a number of tables that have a foreign key to the current version table. These tables also have an implicit foriegn key to the archive table.

I'm trying to figure out how to map this in Hibernate. So far I've found I can't map the related tables to the archived table because the foreign keys do match the primary key on the archive table.

Should I create a custom entity persister? If so, does anyone have tips or examples of how to accomplish this?

Or should I just manually load this? If so, how do I deal with other classes that are related to this class (thru many-to-one or collections)?

Thanks for any ideas.

Regards,
John

Here's a sketch of the problem:

Code:
// the versioned class that needs to work
// with both invoice_item and archive_invoice_item
class InvoiceItem {
   String id;
   Integer sequence;
   List discounts;
}

// the discount class
class Discount {
   Integer id,
   Double percentage
}

table invoice_item {
   id varchar(10) primary key,
   seq int  ## stores the current version number
);

table archive_invoice_item {
  id varchar(10) primary key,
  seq int primary key
);

table discount (
  id int primary key,
  percentage double
);

table invoice_item_discount (
  invoice_item_id varchar(10) primary key,
  discount_id int primary key,
  foreign key fk_invoice_item (invoice_item_id) references invoice_item,
  foreign key fk_discount (discount_id) references discount
);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 6:50 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
I don't know what "implicit foreign key" means. In your sketch, the only foreign keys are to invoice_item, not archive_invoice_item.

Why explicitly map archive_invoice_item at all? How do you do the moving of items from invoice_item to archive_invoice_item? Why not just map invoice_item as class InvoiceItem, and then have archive_invoice_item mapped separately to ArchiveInvoiceItem?

But I probably am missing some SQL subtlety here....
Cheers,
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 9:07 pm 
Beginner
Beginner

Joined: Fri Oct 24, 2003 4:28 pm
Posts: 20
RobJellinghaus wrote:
I don't know what "implicit foreign key" means. In your sketch, the only foreign keys are to invoice_item, not archive_invoice_item.

Why explicitly map archive_invoice_item at all? How do you do the moving of items from invoice_item to archive_invoice_item? Why not just map invoice_item as class InvoiceItem, and then have archive_invoice_item mapped separately to ArchiveInvoiceItem?

But I probably am missing some SQL subtlety here....
Cheers,
Rob


Most items that are related to an InvoiceItem have a foreign key to the archive_invoice_item table so they can have the values from InvoiceItem at the time the item was created. The discounts on an InvoiceItem are not archived so they only have a foreign key to the main invoice item but are also accessed from the archived invoice item. Hibernate will not load the relationship between archive_invoice_item and invoice_item_discount since the primary key on archive_invoice_item has an extra key column(sequence) that is not on the invoice_item_discount table.

Regards,
John


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.