2011-06-26 170 views
0

我試圖去抓住與Hibernate和有問題試圖映射3個實體之間的關係3個實體之間的關係:映射在Hibernate中

比如我有以下類:

分銷商(id,name)例如:TNT,UPS,Fed Ex以及每個經銷商都有一個 ShippingMethod(id,name,description)例如:超級省電,隔夜國際,次日和每種運輸方式有一系列 ShippingOption id,名稱,默認值,活動,成本,可用性),並且這些都是特定分銷商特定的。

要在數據庫中這樣做,我需要一個分發服務器表,一個ShippingMethods表和一個ShippingOptions表,它與分銷商和ShippingMethods具有FK關係。

但是,在我的域模型中,我想要有兩個類--Distributed(),它將包含ShippingOption()類的列表。 ShippingOption()從兩個ShippingMethod()和ShippingOption()相結合的一類(我已經離開了下面簡潔干將等):

@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; 
    } 
} 

是否有可能在Hibernate和如何建模呢?如果您需要更多信息,請告訴我。不知道這是相關的,但我也使用彈簧mvc 3。

乾杯

莫里斯

+0

分銷商,ShippingMethod和ShippingOption之間的關係是什麼。 ManyToOne還是OneToOne? – kalyan

+0

嗨Kaylan需要很長時間才能回覆。分銷商可以有許多運輸選項。每個送貨選項都有一種送貨方式。 – Morrislgn

+0

然後下面的例子將工作,將@ManytoOne更改爲@OneToOne for shippingMethod – kalyan

回答

2

連接三個表是可能的休眠。我有類似的問題在哪裏(在你的例子中)ShippingOption與分銷商和ShippingMethod有ManyToOne關係。

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)) 

我想這就是你的表的樣子。可能是我會回答我解決的問題,你可以找出你的問題。我的POJO看起來像這樣,

@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.  
} 

我得給定的分銷商名稱的所有ShippingOption。 MySQL查詢會是這樣的鏈接此

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'; 

在Hibernate實現,

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(); 

完蛋了,我會得到ShippingOption的列表,其中每個將有其相應的分銷商和ShippingMethod。