hibernate, join table with additional column

流过昼夜 提交于 2020-06-01 02:40:29

问题


I want to save severel Roles for a User. For that purpose I have created a Jointable user2role,which assigns every user id an role id. So far everything works finely. Additionally, I want to save some columns in the join table, for example the the last last_modifying_date (see table defintion below). I dont want to create an additional controller for user2role. I want to solve it by extending the current mapping definiton. Thank you for your help!

The mapping file of User (User.hbm.xml) contains following definition:

    <set name="roles" cascade="all" table="user2role" lazy="false">
        <key column="userID" />
        <many-to-many class="domain.Role"
            column="roleID" />
    </set>

The Table user2role looks like:

    CREATE TABLE `user2role` (
  `userID` int(11) NOT NULL DEFAULT '0',
  `roleID` int(11) NOT NULL DEFAULT '0',
  `modifying_user_db` varchar(50) DEFAULT NULL,
  `modifying_user_appl` varchar(50) DEFAULT NULL,
  `last_modifying_date` datetime DEFAULT NULL,
  PRIMARY KEY (`userID`,`roleID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Verknüpfung User zu Berechtigungsrolle';

回答1:


Refer the following:

http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

http://www.codejava.net/frameworks/hibernate/hibernate-many-to-many-association-with-extra-columns-in-join-table-example

The question is also answered in StackOverflow itself:

Mapping many-to-many association table with extra column(s)



来源:https://stackoverflow.com/questions/36129040/hibernate-join-table-with-additional-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!