• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

3 tables to one .. one is to many relationship question

 
Mark Mirafs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I haven't used hibernate for quite some time now and im presented with this situation.


I have 3 tables with different fields but they are all purchase orders and have a set of order_items. (as seen on image) I have a table purchase_order_items which contains all the items of each purchase order may it be type 1,2,3. How do I map this with the hbm.xml .




I should have a one is to many relationship with table to purchase order items , table 2 to purchase order items , table 3 to purchas order items. Is that possible? how do i configure the purchase order item table.

Thanks in advance
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Speaking as a non-expert...

One approach would be to introduce a super-type. Have a PurchaseOrder entity that has a one-to-many relationship with OrderItem, and then have three subclasses of PurchaseOrder.

I'm not experienced enough with Hibernate to know how to map that off the top of my head, but I think you could map those entities to those tables using the table-per-concrete class strategy.
 
Mark Mirafs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi thanks for the reply..

im no hibernate expert myself

so this super type has three types of purchase order inside it?

then each types of PO has a one to many to the super type?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not, inside it - it would have three subclasses (no quite the same thing). But each one of those subtypes would inherit the one-to-many relationship.

Something like this:

When you map that in Hibernate, as I understand it there are three strategies that you can use. You can map them to a single table (using an extra column to indicate the actual type). You can use a table for the superclass, and then extra tables for each subtype (containing any additional fields that you add in the subclass). Or you use table-per-concrete-class, which I think would match the structure you've got.

Edit: here's an example of the mapping.
 
Mark Mirafs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the idea. I did somewhat the same thing but what I did was create a Table similar to this one



but had the STUDENT_PHONE table contains more columns of the different types that I needed. it now has ID,SUPPLIER_ID,CUSTOMER_ID,ETC_ID that i now have one to many relationships with the item table.

thanks for the idea sir!

 
Mark Mirafs
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by the way I also used this link in aid of this implementation

Hibernate Tutorial
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic