映射Hibernate中3个实体之间的关系

时间:2011-06-26 22:37:55

标签: hibernate spring

我正在尝试处理hibernate,并且在尝试映射3个实体之间的关系时遇到问题:

例如,我有以下类:

经销商(id,name)例如:TNT,UPS,Fed Ex和每个经销商都有选择 ShippingMethod(id,name,description)例如:超级保护,隔夜国际,次日和每种运输方式都有一系列 ShippingOption(ID,名称,默认,有效,成本,可用性)以及每个特定于特定经销商。

要在数据库中执行此操作,我将有一个分销表,一个ShippingMethods表和一个与Distributor和ShippingMethods有FK关系的ShippingOptions表。

但是,在我的域模型中,我希望有两个类--Distributor(),它包含一个ShippingOption()类列表。 ShippingOption()是一个由ShippingMethod()和ShippingOption()组成的类(为了简洁起见,我省略了getters等):

@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中对此进行建模以及如何进行?如果您需要更多信息,请告诉我。不确定这是否相关,但我也使用spring mvc 3。

干杯

莫里斯

1 个答案:

答案 0 :(得分:2)

使用hibernate可以加入三个表。我有类似的问题(在你的例子中)ShippingOption与Distributor和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列表,其中每个都有相应的Distributor和ShippingMethod。