Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Joining three tables is possible with hibernate. I have had similar problem where (In your example) ShippingOption had ManyToOne relation with Distributor and ShippingMethod.</p> <pre><code>Distributor (d_id, name) ShippingMethod (sm_id, name, description) ShippingOption (sp_id, d_id, sm_id, name, default, active, cost, availability, foreign key(d_id), foreign key(sm_id)) </code></pre> <p>I guess this is how your table looks like. May be I will answer the problem I solved and you can figure out yours. My pojo looked like this,</p> <pre><code>@Entity @Table(name = "shipping_option") public class ShippingOption { //..all other columns to retrive @JoinColumn(name = "d_id", referencedColumnName = "d_id") @ManyToOne private ShippingMethod shippingMethod @JoinColumn(name = "sm_id", referencedColumnName = "sm_id") @ManyToOne private Ditributor distributor; //Make sure you have Distributor and ShippingMethod Pojos defined public ShippingMethod getShippingMethod() { return this.shippingMethod; } public void setShippingMethod(ShippingMethod sMethod) { this.shippingMethod = sMethod; } // Getter setter for Distributor and other columns as well goes here. } </code></pre> <p>I have to get all the ShippingOption of a given Distributor name. The mysql query would be something link this</p> <pre><code> select d.d_id, d.name, sm.name, so.name, so.default from Distributor as d, ShippingMethod as sm, ShippingOption as so where d.d_id = so.d_id and so.sm_id = so.sm_id and d.name = 'FedEx'; </code></pre> <p>On the hibernate implementation, </p> <pre><code> public List&lt;ShippingOption&gt; getAllShipingOptionByDistributor(final String distName) { //.... return List&lt;ShippingOption&gt; session.crateCriteria(ShippingOption.class) .createAlias("distributor", "distributor") //distributor is the member of ShippingOption of type Distributor, see the above pojo definition. .add(Restrictions.eq("distributor.name", distName); .createAlias("shippingMethod", "sm") .list(); </code></pre> <p>Thats it, i would get a list of ShippingOption in which each would have its corresponding Distributor and ShippingMethod. </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