2017-12-28 8 views
1

私のドメインモデルを永続化メカニズムから切り離すために、XMLを使用してドメインモデルからdbエンティティにマッピングを設定しています。Map <String、MyValueObject>をxmlでjpaにマップする方法?

public class Tenant { 
    long id; 
    Map<String, AuthApp> authApps; 
    ... 
} 

そして、この値オブジェクト:

public class AuthApp { 
    String authCode; 
    int durationInDays; 
    ... 
} 

値オブジェクトが何のライフサイクル自体を持っていない、それは実体上のdenpendsだ 私はこの実体を持っています。 RDBMSに2つのテーブル "tenant"と "auth_app"を作成します。 このケースでJPA XMLを書く方法を教えてもらえますか?私がこれまでにコード化されてきた XMLは、このようなものです:

<entity class="Tenant"> 
    <table name="tenant"/> 
    <attributes> 
    <id name="id"><generated-value strategy="AUTO" /></id> 
    <element-collection name="authApp"> 
     <map-key name="app_id"/> 
     <collection-table name="auth_app"> 
     <join-column name="tenant_id" referenced-column-name="id"/> 
     </collection-table> 
    </element-collection> 
    </attributes> 
</entity> 

私はそれが正しいですかどうかわからない、とどのように継続します。

私は、JPAプロバイダとしてhibernateを使用しています。

+0

"AuthApp"はおそらくエンティティですか?またはそれを永続化するために属性コンバータを使用していますか? – DN1

+0

@ DN1いいえ、私は上記のように、エンティティではなく値オブジェクトです。そして、私はコンバータを使用していない、私はそれがこの場合に動作することができるのだろうか。 –

+0

JPA対応(エンティティ、埋め込み可能)でない場合は、(@ AuthAppの)複数のフィールドをマップを格納するテーブルの単一の列値に変換するために '@ AttributeConverter'を指定しない限り、永続化できません。 – DN1

答えて

0

解決済み! DN1のコメントのおかげで、私は "価値オブジェクト"がJPA意味論の "埋め込み可能"であることを認識していました。したがって、 "AuthApp"を "embeddable"として定義し、 "map-key"の代わりに "map-key-column"を使用して、完了します。 全体は次のようなものです:

<entity class="Tenant"> 
    <table name="tenant"/> 
    <attributes> 
    <id name="id"><generated-value strategy="AUTO" /></id> 
    <element-collection name="authApp" target-class="AuthApp"> 
     <map-key-column name="app_id"/> 
     <collection-table name="auth_app"> 
     <join-column name="tenant_id" referenced-column-name="id"/> 
     </collection-table> 
    </element-collection> 
    </attributes> 
</entity> 
<embeddable class="AuthApp"> 
    <attributes> 
     ...... 
    </attributes> 
</embeddable> 
...... 
関連する問題