本文介绍了一对多映射有很多可能性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图坚持我的数据,但遇到了困难,希望以一种理智的方式为hibernate工作。



我有两个对象:a频道和用户。用户以多种方式与频道相关联。它可以有5个特殊状态(可以分配多个状态)或正常。这个问题很复杂,因为它有多对多的关系(一个用户可以在多个渠道,一个渠道有多个用户)。



我的想法是有单独的频道和用户表,但有第三个大的地图表。这个大的地图表看起来像这样

  ----------------- -------------------------------------------------- ------- 
| CHANNEL_ID | USER_ID | STATUS1 | STATUS2 | STATUS3 | STATUS4 | STATUS5 |
---------------------------------------------- ----------------------------
| 5 | 10 | 0 | 1 | 0 | 0 | 0 |
---------------------------------------------- ----------------------------

请注意,如果新用户加入一个频道,他们会得到一个新的行。但是,如果他们获得新的状态,他们现有的行就会发生变化。



最让我困惑的是如何在Hibernate中实现类似的东西。我有这样的事情,但我一直在玩这么长时间(现在2天),我甚至不知道它做了什么。

  @ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinTable(name =quackbot_user_map,joinColumns = {
@JoinColumn(name =USER_ID )},inverseJoinColumns = {
@JoinColumn(name =STATUS1)})
protected Set< UserDAOHb> status1s;




  • 有没有更好的方法来做到这一点?

  • 我应该使用专门的地图对象来代表表格并用它来查询吗?
  • / div>

    如果你想映射那个设计,你需要第三个实体,我会打电话给 Participation




    • 一位用户参与度很高; 一位参与者有很多参与者; 通道,一个用户和5个布尔字段(status1到status5)


      但是更正常的(当然更容易查询)设计这个应该只有一个参与实体只有一个状态,并且为每个用户通道状态元组创建一个参与实例。如果你仔细想想,如果可能的状态数量要大得多(或根本不固定),这将是唯一可能的映射(据我所知)。


      I'm trying to persist my data but am having trouble coming up with a schema that will work for hibernate in a sane way

      I have two objects: a channel and user. The user is associated with the channel but in many ways. It can have 5 special statuses (and can be assigned multiple ones) or be normal. The issue is complicated by the fact that its a many to many relationship (a user can be in multiple channels and a channel has multiple users).

      My idea is to have separate channel and user tables but have a third big map table. This big map table would look something like this

      --------------------------------------------------------------------------
      | CHANNEL_ID | USER_ID | STATUS1 | STATUS2 | STATUS3 | STATUS4 | STATUS5 |
      --------------------------------------------------------------------------
      |      5     |    10   |    0    |    1    |    0    |     0   |    0    |
      --------------------------------------------------------------------------
      

      Note that if a new user joins a channel, they get a new row. However if they get a new status, their existing row is changed.

      Whats really confusing me is how to implement something like that in Hibernate. I've got something like this but I've been playing around with this so long (2 days now) I don't even know what it does anymore.

      @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      @JoinTable(name = "quackbot_user_map", joinColumns = {
          @JoinColumn(name = "USER_ID")}, inverseJoinColumns = {
          @JoinColumn(name = "STATUS1")})
      protected Set<UserDAOHb> status1s;
      

      • Is there a better way to do this?
      • Should I use a dedicated map object that represents the table and query with that instead?

      解决方案

      If you want to map that design, you need a third entity, that I'll call Participation.

      • One user has many participations;
      • One channel has many participations;
      • One participation has one channel, one user, and 5 boolean fields (status1 to status5)

      But a more normal (and certainly easier to query) way of designing this would be to have a Participation entity with just one status, and create a Participation instance per User-Channel-Status tuple. If you think about it, it would be the only possible mapping (as far as I see) if the number of possible statuses was much bigger (or not fixed at all).

      这篇关于一对多映射有很多可能性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:17
查看更多