休眠,加入 table 和附加列
hibernate, join table with additional column
我想为用户保存多个角色。为此,我创建了一个 Jointable user2role,它为每个用户 ID 分配一个角色 ID。到目前为止一切正常。此外,我想在联接 table 中保存一些列,例如最后一个 last_modifying_date(请参阅下面的 table 定义)。我不想为 user2role 创建一个额外的控制器。我想通过扩展当前映射定义来解决它。感谢您的帮助!
用户 (User.hbm.xml) 的映射文件包含以下定义:
<set name="roles" cascade="all" table="user2role" lazy="false">
<key column="userID" />
<many-to-many class="domain.Role"
column="roleID" />
</set>
Table user2 角色如下所示:
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/
Whosebug 本身也回答了这个问题:
Mapping many-to-many association table with extra column(s)
我想为用户保存多个角色。为此,我创建了一个 Jointable user2role,它为每个用户 ID 分配一个角色 ID。到目前为止一切正常。此外,我想在联接 table 中保存一些列,例如最后一个 last_modifying_date(请参阅下面的 table 定义)。我不想为 user2role 创建一个额外的控制器。我想通过扩展当前映射定义来解决它。感谢您的帮助!
用户 (User.hbm.xml) 的映射文件包含以下定义:
<set name="roles" cascade="all" table="user2role" lazy="false">
<key column="userID" />
<many-to-many class="domain.Role"
column="roleID" />
</set>
Table user2 角色如下所示:
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/
Whosebug 本身也回答了这个问题:
Mapping many-to-many association table with extra column(s)