Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate one-to-one link table mapping + design suggestion

 
Marcus Hathaway
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

I'm facing some difficulty mapping a one-to-one relationship on a link table. I'm not entirely sure what the best way to go about this is...so any suggestions on mapping or alternative design would be most welcome.

If we imagine this scenario. Let's say we have a table Product and a table PriceFormula. These tables are completely independant of each other. However, a product MAY have a reference to a single PriceFormula (but not always). Therefore, my design would be not to have a reference in the Product table (as not all products will need it) but to introduce a third (link) table ProductPriceFormula. ProductPriceFormula would share the same primary key as Product.

So ProductPriceFormula table would have say two fields ProductID and PriceFormulaID.

In hibernate i'm thinking that ProductPriceFormula would need to be represented as an object? Is this correct? So i couldn't just put a PriceFormula object in the Product object...instead i would need to create a ProductPriceFormula object and hold a reference to that in Product?

I'm thinking my ProductPriceFormula class would look like:

class ProductPriceFormula
{
private Product product;
private PriceFormula priceFormula;

//Getters & Setters
}

Assuming all of the above makes sense...how would one then map this class in hibernate?

I'm thinking the product class mapping would have something like this in for the ProductPriceFormula:

<one-to-one name="productPriceFormula" class="ProductPriceFormula" cascade="save-update"/>

However i can't seem to get the ProductPriceFormula class mapping right

Any tips or comments would be greatly appreciated, thanks!
 
Rashid Darvesh
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marcus
Have a look at this use cases. It doen have a one to one relationship based on foreign key constraint
The only thing to take care of is the use cases have no exception handling which you can put it in your code.
http://www.makemyinfo.com/Tutorial.do?action=getTutorial&tutId=T0005.jsp

Thanks
Rashid
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic