11

私はUserクラスを持っていますLog(私は変更できません):

class User {
    private long id;
    private String name;
    private Set<Log> accessLogs;
    private Set<Log> actionLogs;
}

class Log {
    private String what;
    private Date when;
}

可能なマッピングは次のようになります。

<class name="com.example.User" table="users">
    <id name="id" access="field">
        <generator class="native" />
    </id>

    <property name="name" length="256" />

    <set name="accessLogs" table="user_access_logs" cascade="all-delete-orphan" order-by="`when`">
        <key column="user_id" />
        <composite-element class="com.example.Log">
            <property name="what" length="512" />
            <property name="when" column="`when`" />
        </composite-element>
    </set>

    <set name="actionLogs" table="user_action_logs" cascade="all-delete-orphan" order-by="`when`">
        <key column="user_id" />
        <composite-element class="com.example.Log">
            <property name="what" length="512" />
            <property name="when" column="`when`" />
        </composite-element>
    </set>

</class>

これは正常に機能し、次のデータベーステーブルにマップされます。

users
+----+------+
| id | name |
+----+------+
|  1 | john |
|  2 | bill |
|  3 | nick |
+----+------+

user_access_logs
+---------+------------+---------------------+
| user_id | what       | when                |
+---------+------------+---------------------+
|       1 | logged in  | 2010-09-21 11:25:03 |
|       1 | logged out | 2010-09-21 11:38:24 |
|       1 | logged in  | 2010-09-22 10:19:39 |
|       2 | logged in  | 2010-09-22 11:03:18 |
|       1 | logged out | 2010-09-22 11:48:16 |
|       2 | logged in  | 2010-09-26 12:45:18 |
+---------+------------+---------------------+

user_action_logs
+---------+---------------+---------------------+
| user_id | what          | when                |
+---------+---------------+---------------------+
|       1 | edit profile  | 2010-09-21 11:28:13 |
|       1 | post comment  | 2010-09-21 11:30:40 |
|       1 | edit profile  | 2010-09-21 11:31:17 |
|       1 | submit link   | 2010-09-22 10:21:02 |
|       2 | submit review | 2010-09-22 11:10:22 |
|       2 | submit link   | 2010-09-22 11:11:39 |
+---------+---------------+---------------------+

私の質問は、休止状態でこれら2つのセット(accessLogsおよびactionLogs)を同じテーブルにマップして、ログの次のスキーマを生成する方法を教えてください。

user_logs
+---------+---------------+---------------------+--------+
| user_id | what          | when                | type   |
+---------+---------------+---------------------+--------+
|       1 | logged in     | 2010-09-21 11:25:03 | access |
|       1 | edit profile  | 2010-09-21 11:28:13 | action |
|       1 | post comment  | 2010-09-21 11:30:40 | action |
|       1 | edit profile  | 2010-09-21 11:31:17 | action |
|       1 | logged out    | 2010-09-21 11:38:24 | access |
|       1 | logged in     | 2010-09-22 10:19:39 | access |
|       1 | submit link   | 2010-09-22 10:21:02 | action |
|       2 | logged in     | 2010-09-22 11:03:18 | access |
|       2 | submit review | 2010-09-22 11:10:22 | action |
|       2 | submit link   | 2010-09-22 11:11:39 | action |
|       1 | logged out    | 2010-09-22 11:48:16 | access |
|       2 | logged in     | 2010-09-26 12:45:18 | access |
+---------+---------------+---------------------+--------+

編集:Javaコードを保持し、セマンティクスをそのまま設定したい。私は、Javaコードに触れることなくこれを解決する、ディスクリミネーター、数式、または休止状態のAPI拡張機能のようなものを探しています。たぶん、線に沿った何か(想像上の休止状態の構成が続きます):

<set name="accessLogs" table="user_logs" ... formula="type='access'">
    <key column="user_id" />
    <composite-element class="Log">
        ...
    </composite-element>
</set>

<set name="actionLogs" table="user_logs" ... formula="type='action'">
    <key column="user_id" />
    <composite-element class="Log">
        ...
    </composite-element>
</set>

Edit2:これに対する完全な解決策はまだありません。おそらく休止状態のAPIを拡張することで、それを実行できると確信しています。

4

3 に答える 3

11

この方法でセットをマップしようとしましたか:

<set name="accessLogs" table="user_logs" where="type='access'">    
于 2010-11-05T17:29:13.360 に答える
5

Maurizio が書いたように、where属性を使用して、各コレクションに属する行のみを取得する必要があります。

追加の列を使用して行を挿入<set>するには、 内に次の要素を追加する必要があります。

<sql-insert>
  insert into user_logs(user_id, what, [when], type)
  values(?, ?, ?, 'access')
</sql-insert>

これは基本的に、Hibernate に特定の SQL を生成する代わりに使用するように指示します。when列を手動でエスケープする必要があることに注意してください。

おまけ: DB に追加の列を追加するには、次を使用します (を閉じた後<class>)

<database-object>
  <create>alter table user_logs add type char(6)</create>
  <drop></drop>
</database-object>

興味深い点: NHibernateを使用してこれを作成およびテストしましたが、直接移植されたので、まったく同じように動作するはずです。

于 2010-11-14T23:13:23.913 に答える
1

リストを 1 つのリストに結合するだけではうまくいかないのはなぜですか? あなたのリストはどちらも同じクラスです。

編集 - 2 つのリストを保持したい場合は、組み合わせである 3 番目のリストを作成し、それのみを保持します。2 つのリストのアクセサーへのアクセスを制御する必要があるため、3 番目のリストをいつ更新するかがわかりますが、それほど難しくはありません。

于 2010-11-05T15:07:01.730 に答える