Note that there are some explanatory texts on larger screens.

plurals
  1. POMapping a relationship between 3 entities in Hibernate
    text
    copied!<p>I am trying to get to grips with hibernate and have a problem trying to map a relationship between 3 entities:</p> <p>For example I have the following classes:</p> <p>Distributor (id, name) e.g.: TNT, UPS, Fed Ex and each distributor has a selection of ShippingMethod (id, name, description) e.g.: Super saver, overnight international, Next Day and each shipping method has a series of ShippingOption (id, name, default, active, cost, availability) and each of these are specific to a particular distributor.</p> <p>To do that in a DB I would have a table for Distributor, a table for ShippingMethods and a table for ShippingOptions which would have a FK relationship to Distributor and ShippingMethods. </p> <p>However, in my domain model I would like to have two classes - Distributor() which would contain a list of ShippingOption() classes. ShippingOption() is a class combined from both ShippingMethod() and ShippingOption() (I've left out getters etc for brevity below): </p> <pre><code>@Entity @Table(name = "shipping_option") public class ShippingOption { @Column(name = "is_active", nullable = false) private boolean isActive = false; @Column(name = "is_default", nullable = false) private boolean isDefault = false; @Column(name = "cost", nullable = false) private BigDecimal cost = BigDecimal.ZERO; private ShippingMethod shippingMethod = new ShippingMethod(); public ShippingOption() { } public ShippingOption(boolean isDefault, BigDecimal cost, ShippingMethod shippingMethod) { setDefault(isDefault); cost(cost); setShippingMethod(shippingMethod); } public void setDefault(boolean isDefault) { this.isDefault = isDefault; } public void setShippingMethod(ShippingMethod shippingMethod) { this.shippingMethod = shippingMethod; } } </code></pre> <p>Is it possible to model this in Hibernate and how? If you need more information please let me know. Not sure if this is relevent but I am using spring mvc 3 also.</p> <p>Cheers</p> <p>Morris</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload