0

このマッピングの問題について教えてください(私はHibernate 3.6を使用しています)。

私はこれらのテーブルを持っています

CREATE  TABLE Towers (
  Id INT NOT NULL AUTO_INCREMENT ,
  Name VARCHAR(5) NOT NULL ,
  PRIMARY KEY (Id));

CREATE  TABLE LineOfBusiness (
  Id INT NOT NULL AUTO_INCREMENT,
  Name VARCHAR(15) NOT NULL ,
  PRIMARY KEY (Id));

CREATE  TABLE LineOfBusinessXTowers (
  LineOfBusinessId INT NOT NULL ,
  TowerId INT NOT NULL ,
  PRIMARY KEY (LineOfBusinessId, TowerId));

CREATE  TABLE IF NOT EXISTS Departments (
  Code INT NOT NULL ,
  LineOfBusinessId INT NOT NULL ,
  TowerId INT NOT NULL ,
  PRIMARY KEY (Code));

ALTER TABLE LineOfBusinessXTowers
  ADD CONSTRAINT FK_LineOfBusinessXTowers_LineOfBusiness
    FOREIGN KEY (LineOfBusinessId)
    REFERENCES LineOfBusiness (Id)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  ADD CONSTRAINT FK_LineOfBusinessXTowers_Towers
    FOREIGN KEY (TowerId)
    REFERENCES Towers (Id)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION;

ALTER TABLE Departments
  ADD CONSTRAINT FK_Departments_LineOfBusinessXTowers
    FOREIGN KEY (LineOfBusinessId, TowerId)
    REFERENCES LineOfBusinessXTowers (LineOfBusinessId,TowerId)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION;

Hibernateマッピングクラスに、対応するTowerオブジェクトとLineOfBusinessオブジェクトを持つDepartmentクラスが必要です。

public class Departments {
        private int code;
   private String name;
   private LineOfBusiness lineOfBusiness;
   private Towers tower;

       //Constructors and access methods here
}

私はこの方法でマッピングを行おうとしていましたが、間違っています:

<hibernate-mapping>
   <class name="Departments" table="Departments" catalog="DB">
      <id name="code" type="int">
         <column name="Code" />
         <generator class="assigned" />
      </id>
      <property name="name" type="string">
         <column name="Name" length="45" not-null="true" unique="true" />
      </property>

      <join table="LineOfBusinessXTowers" optional="false">
         <key column="LineOfBusinessId" unique="false" />
         <many-to-one name="lineOfBusiness"
            class="LineOfBusiness">
            <column name="LineOfBusinessId" not-null="true" />
         </many-to-one>
      </join>
      <join table="LineOfBusinessXTowers" optional="false">
         <key column="TowerId" unique="false" />
         <many-to-one name="towers" class="Towers">
            <column name="TowerId" not-null="true" />
         </many-to-one>
      </join>
   </class>
</hibernate-mapping>

私がやりたいことをするための正しいマッピングはどれですか?できますか?別のより良い戦略はありますか?

どうもありがとうございました。

アテ:エドガー

4

1 に答える 1

0

それ以外の

<join table="LineOfBusinessXTowers" optional="false">
  <key column="LineOfBusinessId" unique="false" />
  <many-to-one name="lineOfBusiness" class="LineOfBusiness">
    <column name="LineOfBusinessId" not-null="true" />
  </many-to-one>
</join>

行う

<many-to-one name="lineOfBusiness" class="LineOfBusiness">
  <column name="LineOfBusinessId" not-null="true" />
</many-to-one>

タワーへのマッピングは簡単にはできません。Join は primaryKey/Id での結合のみをサポートします。

オプション 1: lineOfBusiness にタワーが 1 つしかない場合

// in department
public Towers getTower()
{
    return lineOfBusiness.getTower();
}

// in LineOfBusiness mapping
<join table="LineOfBusinessXTowers" optional="false">
  <key column="LineOfBusinessId" unique="false" />
  <many-to-one name="tower" column="TowerId" />
</join>

オプション 2: lineOfBusiness に多くのタワーがある場合

// in department
public Towers getTower()
{
    return lineOfBusiness.getTowers().getAt(0);
}

// in LineOfBusiness mapping
<set table="LineOfBusinessXTowers" optional="false">
  <key column="LineOfBusinessId" unique="false" />
  <many-to-many name="tower" column="TowerId" />
</join>

オプション 3: manytomany テーブルのエンティティを導入する

class LineOfBusinessAndTower
{
    private LineOfBusiness lineOfBusiness;
    private Tower tower;

    // override equality to take the two references into account
}

// in LineOfBusinessAndTower mapping
<Composite-id >
  <key-many-to-one name="lineOfBusiness" column="LineOfBusinessId" />
  <key-many-to-one name="tower" column="TowerId" />
</join>

// in Department mapping
<manytoone table="internalLineOfBusinessXTowers">
  <column="LineOfBusinessId" />
  <column="TowerId" />
</manytoone>

// in Department
public LineOfBusiness getLineOfBusiness()
{
    return internalLineOfBusinessXTowers.getLineOfBusiness();
}

public Towers getTower()
{
    return internalLineOfBusinessXTowers.getTower();
}
于 2012-07-13T06:26:15.507 に答える