Mapping a relationship between 3 entities in Hibernate
I am trying to get to grips with hibernate and have a problem trying to map a relationship between 3 entities:
For example I have the following classes:
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.
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.
However, in my domain model I would like to have two classes - Distributor() which would contain a list of ShippingOption() classes. Shi开发者_运维百科ppingOption() is a class combined from both ShippingMethod() and ShippingOption() (I've left out getters etc for brevity below):
@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;
}
}
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.
Cheers
Morris
Joining three tables is possible with hibernate. I have had similar problem where (In your example) ShippingOption had ManyToOne relation with Distributor and ShippingMethod.
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))
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,
@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.
}
I have to get all the ShippingOption of a given Distributor name. The mysql query would be something link this
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';
On the hibernate implementation,
public List<ShippingOption> getAllShipingOptionByDistributor(final String distName) {
//....
return List<ShippingOption> 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();
Thats it, i would get a list of ShippingOption in which each would have its corresponding Distributor and ShippingMethod.
精彩评论