2011-10-18 15 views
0

JPAを使用するように、Hibernateの設定を変換しています。現在の構成には、ALERT_CATEGORY表の主キー値であるLong型のコレクションを持つAlertPrefクラス(ALERT_PREF表)があります。この2つを結合するALERT_PREF_CATEGORYジャンクション表があります。ジャンクションテーブルをEntityクラスとして定義し、AlertPrefクラスのLong IDの代わりにAlertPrefCategoryオブジェクトのコレクションを持つことでJPAでこの関係をセットアップできましたが、これを可能な限り避けたいのではなく、一方向マッピングをロングIDへ​​のAlertPref。レガシーコードの中にはIDを使用するものがあり、このコードを変更することは困難です。ここでJPAを使用してラッパーオブジェクトに単方向のOneToManyアソシエーションをマッピングする

が正常に動作Hibernateの設定で現在のクラスのタグです:ここで

<class name="AlertPref" table="ALERT_PREF"> 
    <id name="alertPrefId" column="ALERT_PREF_ID" type="long"> 
     <generator class="hilo"> 
      <param name="max_lo">100</param> 
     </generator> 
    </id> 

    <property name="userId" column="USER_ID" type="string" 
     not-null="true" /> 

    <set name="excludedCategoryIds" table="ALERT_PREF_CATEGORY" cascade="all,delete-orphan"> 
     <key column="ALERT_PREF_ID" /> 
     <element column="EXCLUDED_CATEGORY_ID" type="long" /> 
    </set> 

</class> 

は、私はJPAで使用しようとしたものですが、それは@OneToManyまたは標的@ManyToManyの例外」の使用を投げていますマップされていないクラス:AlertPref.excludedCategoryIds [java.lang.Longの]」

@Entity 
@Table(name = "ALERT_PREF") 
public class AlertPref { 
    @Id 
    @TableGenerator(name = "table_gen", allocationSize = 1) 
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "table_gen") 
    @Column(name = "ALERT_PREF_ID") 
    private long alertPrefId; 

    @Column(name = "USER_ID", nullable = false) 
    private String userId; 

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) 
    @JoinTable(name = "ALERT_PREF_CATEGORY", 
     joinColumns = @JoinColumn(name = "ALERT_PREF_ID"), 
     inverseJoinColumns = @JoinColumn(name = "EXCLUDED_CATEGORY_ID")) 
    private Set<Long> excludedCategoryIds; 

    /** 
    * @return Returns the alertPrefId. 
    */ 
    public long getAlertPrefId() { 
     return alertPrefId; 
    } 

    /** 
    * @param alertPrefId 
    *   The alertPrefId to set. 
    */ 
    public void setAlertPrefId(long alertPrefId) { 
     this.alertPrefId = alertPrefId; 
    } 

    /** 
    * @return Returns the userId. 
    */ 
    public String getUserId() { 
     return userId; 
    } 

    /** 
    * @param userId 
    *   The userId to set. 
    */ 
    public void setUserId(String userId) { 
     this.userId = userId; 
    } 

    /** 
    * @return the excludedCategoryIds 
    */ 
    public Set<Long> getExcludedCategoryIds() { 
     return excludedCategoryIds; 
    } 

    /** 
    * @param excludedCategoryIds the excludedCategoryIds to set 
    */ 
    public void setExcludedCategoryIds(Set<Long> excludedCategoryIds) { 
     this.excludedCategoryIds = excludedCategoryIds; 
    } 
} 

答えて

0

Hibernate reference documentationで説明したようにあなたは、ElementCollectionとCollectionTable注釈を使用する必要があります。

+0

カスケード操作を実行する場合、接合テーブルを表すためにエンティティを定義する必要があると思います。正しいのですか? – acvcu

+0

いいえ。基本タイプまたは組み込みコンポーネントを含むコレクションは、それを保持するエンティティと同じライフサイクルを持ちます。したがって、IDを含むAlertPrefを作成すると、結合テーブルに行が作成されます。セットを変更すると、それに応じて行が変更されます。また、エンティティを削除すると、結合表の行が削除されます。 –

+0

カスケードと孤立した削除をどのように通知できますか?これらはElementCollectionまたはCollectionTableのオプションではありません。 AlertPref列を削除すると、AlertPrefを参照しているAlertPrefCategoryの列を削除して、参照整合性を維持します。 – acvcu

関連する問題