7

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';
Volkan Trabzon
  • 75
  • 1
  • 1
  • 6

1 Answers1

5

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)

Community
  • 1
  • 1
ajoshi
  • 349
  • 2
  • 10
  • 1
    These solutions are based on a new controller. Do you think it would be possible to realize it without creating a new controller ? – Volkan Trabzon Mar 21 '16 at 12:21
  • I have tried your approach up to hibernate 3.6 and it does not work. I am not sure about later versions. – ajoshi Mar 21 '16 at 13:27