我想为用户保存严重的角色。为此,我创建了一个 Jointable user2role,它为每个用户 ID 分配一个角色 ID。到目前为止,一切正常。此外,我想在连接表中保存一些列,例如最后一个 last_modifying_date(参见下面的表定义)。我不想为 user2role 创建额外的 Controller 。我想通过扩展当前的映射定义来解决它。感谢您的帮助!
User的映射文件(User.hbm.xml)包含如下定义:
<set name="roles" cascade="all" table="user2role" lazy="false">
<key column="userID" />
<many-to-many class="domain.Role"
column="roleID" />
</set>
表 user2role 看起来像:
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';
最佳答案
请引用以下内容:
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
StackOverflow 本身也回答了这个问题:
Mapping many-to-many association table with extra column(s)
关于 hibernate ,将表与附加列连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36129040/