Hibernate列表映射在一个单独的表中

时间:2013-09-26 13:03:31

标签: java mysql hibernate orm

我们有两个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

<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正在做的是在idx中维护notification_iduser  表。我希望hibernate应该使用列notification_foruser_id维护一个表notification_id我应该怎么做。有什么建议吗?

1 个答案:

答案 0 :(得分: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>