简体   繁体   中英

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.

this is the mapping of the 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.

The problem is that when i try to delete "moderator" from the sub_forum`s set, this action is not reflected in DB. (when i am adding new moderator to the same list, there is association in the subforums_moderators as expected).

when i remove the whole sub-forum, all the association in the subforums_moderators removed also.

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" 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.

  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) .

    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. 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. 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. and then you can change user.subforumId to null where user.subforumId = deletedSubforumId and then delete the subforum

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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