【问题标题】:mapping multiple sets in one table in hibernate在休眠中将多个集合映射到一个表中
【发布时间】:2011-05-05 15:46:39
【问题描述】:

我有一个 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 个集合(accessLogsactionLogs)映射到同一个表中,从而为日志生成以下架构:

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 代码并按原样设置语义。我正在寻找诸如鉴别器、公式或休眠 API 扩展之类的东西,它们可以在不触及 Java 代码的情况下解决这个问题。也许是这样的(想象中的休眠配置如下):

<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 来完成。

【问题讨论】:

    标签: java hibernate hibernate-mapping


    【解决方案1】:

    为什么将列表合并为一个列表不起作用?您的两个列表都属于同一类。

    编辑 - 如果您想保留 2 个列表,则创建第三个列表作为组合,并且只保留它。您必须控制对这两个列表的访问器的访问,因此您知道何时更新第三个列表,但应该不会太难。

    【讨论】:

    • 我想通过更改休眠映射代码来解决这个问题。
    • @cherouvim 如果您有 2 个集合,则需要映射这些集合。如果你想要一套,你必须改变你的代码。
    • 我需要 2 个集合,我可以映射它们,但我希望它们映射到单个表。
    • @cherouvim,然后尝试创建第三组,就像我在回答中建议的那样,并映射它——然后你有一个持久集,你的域逻辑在这两组上工作......
    • 第三组可以完成这项工作,但我需要知道这是否只能通过配置或休眠扩展来解决。
    【解决方案2】:

    您是否尝试过以这种方式映射集合:

    <set name="accessLogs" table="user_logs" where="type='access'">    
    

    【讨论】:

    • 谢谢,太棒了。现在我只需要找到一种方法将“访问”注入到我的模型中不存在的类型列中。
    • +1 这是我在类似情况下所做的,它工作正常。
    • 是的,但我无法区分访问日志和操作日志,因为 user_logs 表只有 user_idwhatwhen
    • 我需要通过休眠配置来完成。我无法修改或扩展我的模型。
    • 那么只有一种方法可以链接 where 条件:where="what = 'logged in' or what='what else'"
    【解决方案3】:

    正如 Maurizio 所写,您需要使用 where 属性来仅检索属于每个集合的行。

    要使用附加列插入行,您需要在&lt;set&gt;内添加以下元素:

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

    这实质上是告诉 Hibernate 使用特定的 SQL 而不是生成它。请注意,我必须手动转义 when 列。

    奖励:要将额外的列添加到 DB,请使用以下命令(关闭 &lt;class&gt; 后)

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

    有趣的一点:我使用 NHibernate 编写并测试了它,但它是直接移植的,所以应该完全一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 2023-03-14
      相关资源
      最近更新 更多