2

I have to create complex hibernate mapping. The following simplified example explains my problem.

I have two entities:

public class Work {

  private WorkType type;
  private Set<Workers>;
  ...
}

public class Worker {

  private Map<WorkType,Work>;
  ...
}

I have 3 tables:

t_works columns: id, type,...
t_workers columns: id,...
t_work_worker columns: worker_id, work_id.

I want to map the map with hibernate, without copying the type values to t_work_worker. The problem here is that the map key (WorkType) is a part of the map value (Work).

My hibernate hbm:

<typedef class="org.hibernate.type.EnumType" name="workType">
   <param name="enumClass">myPackage.WorkType</param> 
   <param name="type">12</param> 
</typedef>

<class name="work" table="T_WORKS">
    <property name="type" type="workType" column="type"/>
    <set name="workers" table="T_WORK_WORKER" inverse="true" lazy="false" cascade="none">
        <key column="WORK_ID" />
        <many-to-many column="WORKER_ID"class="myPackage.Worker"/>
    </set>
</class>

<class name="Worker" table="T_WORKERS">
    <map name="channels" table="T_WORK_WORKER" lazy="false" cascade="all">
       <key column="WORKER_ID" />                        
       <map-key formula="(select w.TYPE from t_works w where w.ID=WORK_ID)"type="workType"/>
       <many-to-many column="WORK_ID" class="myPackage.Work"/>
    </map>
</class>

This mapping works but requires additional select statement (see formula atribute). I wonder whether there's a way to map the work type as a key, without using "formula" and without adding the type column to the relations table.

4

1 に答える 1

1

あなたが言ったように、問題の一部は、循環参照があることです。通常、これはあまり良い考えではないため、別の構造を考えてみてください。循環参照は本当に必要ですか? なぜ労働者はワークタイプと仕事のマップを持っている必要があるのですか? おそらく、worker と work を別のクラスで接続するので、循環参照は必要ありません。

于 2012-09-25T12:25:32.913 に答える