aspose file tools*
The moose likes Object Relational Mapping and the fly likes 3 tables to one .. one is to many relationship question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "3 tables to one .. one is to many relationship question" Watch "3 tables to one .. one is to many relationship question" New topic
Author

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

Mark Mirafs
Greenhorn

Joined: Jan 28, 2012
Posts: 4
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

Joined: Apr 06, 2010
Posts: 4464
    
    8

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

Joined: Jan 28, 2012
Posts: 4
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

Joined: Apr 06, 2010
Posts: 4464
    
    8

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

Joined: Jan 28, 2012
Posts: 4
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

Joined: Jan 28, 2012
Posts: 4
by the way I also used this link in aid of this implementation

Hibernate Tutorial
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: 3 tables to one .. one is to many relationship question