将表与其他列连接

将表与其他列连接

本文介绍了休眠,将表与其他列连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为用户保存严格的角色.为此,我创建了一个Jointable user2role,它为每个用户ID分配一个角色ID.到目前为止,一切正常.另外,我想在联接表中保存一些列,例如最后的last_modifying_date(请参阅下面的表定义).我不想为user2role创建其他控制器.我想通过扩展当前的映射定义来解决它.谢谢您的帮助!

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!

用户(User.hbm.xml)的映射文件包含以下定义:

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>

表user2role如下:

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';

推荐答案

请参阅以下内容:

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本身也回答了这个问题:

The question is also answered in StackOverflow itself:

用多列映射多对多关联表(s)

这篇关于休眠,将表与其他列连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 04:40