【问题标题】:Hibernate list mapping in a separate table单独表中的休眠列表映射
【发布时间】:2013-10-02 11:20:18
【问题描述】:

我们下面有两个 POJO

public class Notification extends AbstractDomainObject{

private String name;
private List<User> users;
//setter getter

}

public class User extends AbstractDomainObject{

private String userName;
//getter setter
}

public class AbstractDomainObject{
private Long id;
//getter setter

}

我的 Hibernate 映射是

<hibernate-mapping >
    <class name="User" table="user">
        <id name="id" type="long" column="id">
            <generator class="native" />
        </id>

        <property name="userName" type="string">
            <column name="user_name" />
        </property>

    </class>

<class name="Notification " table="notification">
        <id name="id" type="long" column="id">
            <generator class="native" />
        </id>

        <property name="name" type="string">
            <column name="name" />
        </property>
        <list name="users" table="notification_for" cascade="all-delete-orphan" >
   <key column="notification_id" />
   <list-index column="idx" />

   <one-to-many class="User" />
  </list>
    </class>
</hibernate-mapping>

我希望 hibernate 维护一个单独的表 (notification_for) 用于映射用户和通知。

hibernate 所做的是在user 中维护idxnotification_id 桌子。我希望hibernate应该维护一个表notification_for与列user_idnotification_id我该怎么办。有什么建议吗?

【问题讨论】:

    标签: java mysql hibernate orm


    【解决方案1】:

    使用多对多关系:

    在用户端...

    <bag name="notifications" inverse="true" table="NOTIFICATIONS_FOR">
      <key>
        <column name="USER_ID" not-null="true"/>
      </key>
      <one-to-many class="Notification"/>
    </bag>
    

    然后在通知方面..

    <bag name="users" inverse="false" table="NOTIFICATIONS_FOR">
          <key>
        <column name="notification_id" not-null="true"/>
      </key>
      <one-to-many class="Notification"/>
    </bag>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 2023-03-14
      • 2013-11-03
      • 2021-10-16
      相关资源
      最近更新 更多