Search code examples
mysqlsqlhibernatemany-to-manyentity-relationship

Providing timestamp in reference table (many to many relationship)


Consider following table (reference table between tables A and B, many to many relationship):

A_ID int (FK),
B_ID int (FK),
change_date timestamp - represents timestamp when this relation has been created. 

What is the best solution to provide this timestamp? When I perform insert (actually Hibernate performs), Hibernate knows only about A_ID and B_ID.

The only solution that sounds reasonable at the moment is trigger. What do you think?


Solution

  • If you wish to add data to a many-to-many relationship table, just map this table as an entity, and manage it as an entity.

    in A and B, the mapping goes from a @ManyToMany to a @OneToMany

    in the new entity say AB, you map A and B with @ManyToOne and @Id (both fields).

    Have a look at the Example 134 (Bidirectional many-to-many with link entity) in the hibernate reference documentation.