• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

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

Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

Everyone is a villain in someone else's story. Especially this devious tiny ad:
a bit of art, as a gift, that will fit in a stocking
    Bookmark Topic Watch Topic
  • New Topic