3

我们下面有两个 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-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 正在做的是维护idxnotification_iduser 表中。我希望hibernate应该维护一个notification_for带有列的表user_idnotification_id 我应该怎么做。有什么建议吗?

4

1 回答 1

0

使用多对多关系:

在用户端...

<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>
于 2013-09-28T21:15:57.707 回答