简体   繁体   中英

update additional fields in the mapping table using hibernate

I have user table with user.hbm.xml and department table with department.hbm.xml. mapping table is userdepartment with user_id and department_id columns. The mappings are automatically populated through foreign key definitions. I would like to have additional columns in this mapping table that tracks, who changed the mapping, comments etc. How can I update these additional columns? Do I need userdepartment.hbm.xml for this and a mapping class? Is there a way to map these additional columns to main classes? Pls advice.

If the join table has other columns than just the two foreign keys, then it's not a join table anymore, but an entity in itself.

You should thus have an additional Participation entity, containing the information about the participation of a user in a department, and have a OneToMany between User and Participation, and a OneToMany between Department and Participation.

Treat the Participation entity like all the other entities: it should have a single-column auto-generated ID, and two join columns. To make sure the same user doesn't participate twice for the same department, add a unique constraint on [userId, departmentId] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM