2009-05-14 12 views
7

マッピングテーブルを介してテーブルが自己参照する@ManyToManyマッピングがありますが、実際のマッピングテーブルのオーダーIDで注文したいが、これを設定するのは難しい。Hibernateでテーブル値をマッピングすることによる順序付け

JPAアノテーションにサポートがあると仮定すると、それはハイバネートXMLで実行できます。マッピングテーブルの値をどのように注文するかは誰にも分かりますか?

表は以下のとおりです。

wap_site_components 

intid 
strname 
intcomponentdef 
dtmcreated  
intcustomer 

とマッピングテーブルの自己参照があること:私たちが持っているHibernateの注釈で

wap_site_component_relations 

intid  
intparent (references intid in wap_site_components) 
intchild (references intid in wap_site_components) 
intorder (this is the value we want to order the collection on) 

@ManyToMany (fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
@JoinTable (name = "wap_site_component_relations", 
    joinColumns = {@JoinColumn (name = "intparent", referencedColumnName = "id") }, 
    inverseJoinColumns = {@JoinColumn (name = "intchild", referencedColumnName = "id") }) 
public Set<WapComponent> getChildren() { 
    return children; 
} 

これは、我々が行ってどのようですそれは冬眠中のxml:

 <set 
      name="children" 
      table="wap_site_component_relations" 
      lazy="true" 
      cascade="none" 
      sort="unsorted" 
      order-by="intorder" 
      mutable="false" 
     > 
      <cache 
      usage="read-only" 
      /> 

      <key 
      column="intparent" 
      > 
      </key> 

      <many-to-many 
      class="se.plusfoursix.im46.data.wap.WapComponent" 
      column="intchild" 
      outer-join="auto" 
      /> 

     </set> 

@OrderByタグを使用しますが、マッピングテーブルのintorder値を参照することはできません。何か案は?ありがとう。

(私たちは、コード内で子どもたちのコレクションを@OrderBy(intorder)を試してみましたが、それが私たちのために働いていない)

+1

で行うことができます。 – bowsie

答えて

0

マッピングテーブルをBeanとして作成しただけです。

誰もがこれを実行せずに上記を実行する方法を持っている場合は、まだあなたから聞いて嬉しいです!

+0

私は同じ問題を抱えており、同じ方法を解決することに頼ってきました。 – Heathen

+0

これは心配しています。 –

2

あなたは@OneToManyを使用し、その後、リンクテーブルを表す注釈付きオブジェクトを作成することができます親から子へのリンク、次に子供へのリンク

@Entity 
public class Parent { 
    @id 
    Long id; 

    @OneToMany(targetEntity = ChildLink.class) 
    Set<ChildLink> childLinks; 
} 

public class ChildLink { 

    @Id 
    @OrderBy 
    Long orderBy; 

    @ManyToOne 
    Set<Parent> parents; 

    @ManyToOne 
    Set<Child> children; 
} 

ちょうどおおまかな例です。次に、親クラスのgetChildrenメソッドの中で、childLinksのセットからセットされた子をプログラム的に生成することができます。

0

は、私はまた、我々はマッピングテーブルにBeanを作成することによってこれを行うことができることをここで追加する必要がありますが、これを実行したくない@OrderColumn

http://docs.oracle.com/javaee/6/api/javax/persistence/OrderColumn.html?is-external=true

@Entity 
public class CreditCard { 

    @Id long ccNumber; 

    @OneToMany // unidirectional 
    @OrderColumn 
    List<CardTransaction> transactionHistory; 
    ... 
} 
関連する問題