简体   繁体   English

休眠:单向,删除多对多关联

[英]Hibernate: unidirectional, remove many-to-many association

this is my scenario, 这是我的情况

i have SubForum class, that holds set of moderators, each entity int that set is User class. 我有SubForum类,其中包含一组主持人,该组的每个实体都是User类。

this is the mapping of the SubForum 这是SubForum的映射

<class name="server.Subforum">
    <id name="id" type="int">
        <column name="subforum_id"></column>
        <generator class="identity" />
    </id>
    <properties name="unique_subforum" unique="true">
        <property name="name" type="java.lang.String">
            <column name="NAME" not-null="true" />
        </property>
        <property name="forumId" type="int" access="field">
            <column name="forum_id"></column>
        </property>
    </properties>
    <set name="moderators" table="subforums_moderators"
        lazy="false" fetch="select" >
        <key>
            <column name="SUBFORUM_ID" not-null="true" />
        </key>
        <many-to-many column="user_id" class="server.User" />
    </set>
</class>

this is the User mapping: 这是用户映射:

<class name="server.User">
    <id name="id" type="int" access="field">
        <column name="user_id"></column>
        <generator class="identity" />
    </id>
    <property name="username" type="java.lang.String">
        <column name="USERNAME" not-null="true" />
    </property>
    <property name="password" type="java.lang.String">
        <column name="PASSWORD" />
    </property>
    <property name="registrationDate" type="java.util.Date">
    </property>
</class>

that mapping generated the tables as expected, the subforums, users, subforums_moderators, with all the right configuration. 该映射按预期配置,子论坛,用户,subforums_moderators以及所有正确的配置生成了表。

The problem is that when i try to delete "moderator" from the sub_forum`s set, this action is not reflected in DB. 问题是,当我尝试从sub_forum的集合中删除“主持人”时,此动作未反映在DB中。 (when i am adding new moderator to the same list, there is association in the subforums_moderators as expected). (当我将新的主持人添加到同一列表中时, subforums_moderators中存在预期的关联)。

when i remove the whole sub-forum, all the association in the subforums_moderators removed also. 当我删除整个子论坛时, subforums_moderators中的所有关联也都删除了。

what i am doing wrong? 我做错了什么?

Change <set name="moderators" table="subforums_moderators" lazy="false" fetch="select" > to this : <set name="moderators" table="subforums_moderators" lazy="false" fetch="select" >更改为此:

 <set name="moderators" table="subforums_moderators"
        lazy="false" cascade="delete" fetch="select" >
  1. Your original mapping files are fine. 您的原始映射文件很好。 You do not want cascade="delete" - a User should exist in the DB independently of whether they are the moderator for a particular forum. 您不希望cascade="delete" -数据库中应该存在一个用户,而无论他们是否是特定论坛的主持人。

  2. * now when i add user, (its adds to the DB), delete him (it deletes in the DB), and add the same one again, there is no association to him * 现在,当我添加用户时,(将其添加到数据库中),将其删除(在数据库中删除),然后再次添加相同的用户,则与他没有关联

    You need to re-add the same user, and then you need to reassociate that user with a subforum: subforum.addModerator(user) or subforum.getModerators().add(user) . 您需要重新添加同一用户,然后需要将该用户与子论坛重新关联: subforum.addModerator(user)subforum.getModerators().add(user)

    In fact, users, subforums and moderator associations should be created, updated and deleted independently. 实际上,应该独立创建,更新和删除用户,子论坛和主持人关联。 Typical workflow: 典型的工作流程:

      after app functionality DB operation DB tables A. - add user(s) Insert USERS BA add subforum with starting moderator(s) (link(s) to user(s)) Insert SUBFORUMS, SUBFORUMS_MODERATORS CB add moderator(s) to a subforum Insert SUBFORUMS_MODERATORS DB remove moderator(s) from a subforum Delete SUBFORUMS_MODERATORS EB remove a subforum Delete SUBFORUMS_MODERATORS, SUBFORUMS FA semove a user that is not a moderator of any subforum Delete USERS 
  3. also, when i delete the entire subforum, it tries to delete the user it self, but i don't want it...i want only to delete the association 另外,当我删除整个子论坛时,它会尝试自行删除用户,但是我不想要它...我只想删除关联

    That's a cascaded delete from subforum to user. 这是从子论坛到用户的级联删除。 Don't use cascaded delete here. 不要在此处使用级联删除。 Instead, remove the association via subforum.getModerators().remove(user) and then delete the subforum. 而是通过subforum.getModerators().remove(user)删除关联,然后删除该子论坛。 You are responsible for manually coding data changes to associations and entities (two places). 您负责手动将数据更改编码为关联和实体(两个位置)。 If you change just one of these, then hibernate doesn't automatically populate/clear the other. 如果仅更改其中之一,则休眠不会自动填充/清除另一个。

that is because you set the sub forum_id in user to not null. 那是因为您将用户中的sub forum_id设置为不为空。 so you can't delete the sub forum that is used in user. 因此您不能删除用户中使用的子论坛。 so you must change your mapping and recreate your table, or just change the sub forum manually in your database so the user.subforum_id can be nullable. 因此,您必须更改映射并重新创建表,或者只是在数据库中手动更改子论坛,以便user.subforum_id可以为空。 and then you can change user.subforumId to null where user.subforumId = deletedSubforumId and then delete the subforum 然后可以将user.subforumId更改为null,其中user.subforumId = DeletedSubforumId,然后删除子论坛

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM