Many to many relationships with attributes using annotations

enter image description here

I want to know how to create a role_permission table with @ManyToMany annotation given the created_at attribute in the role_permission table.

I know I can do something like this:

public class Role implements Serializable{ @Id @Column(name = "_id") private String id; @Column(name = "name") @NotNull private String name; @Column(name = "description") private String description; @ManyToMany(cascade = {CascadeType.ALL}) @JoinTable(name="role_permission", schema=joinColumns={@JoinColumn(name="idRole")}, inverseJoinColumns={@JoinColumn(name="idPermission")}) private Set<Permission> permissions=new HashSet(); 

and

 public class Permission implements Serializable{ @Id @Column(name = "_id") private String id; @Column(name = "name") @NotNull private String name; @Column(name = "description") private String description; @ManyToMany(cascade = {CascadeType.ALL}) @JoinTable(name="role_permission", schema=joinColumns={@JoinColumn(name="idPermission")}, inverseJoinColumns={@JoinColumn(name="idRole")}) private Set<Permission> roles=new HashSet(); 

With this, I can avoid creating a new class for role_permission, but I don't know how to put created_at on this annotation. Is it possible?

+6
source share
1 answer

I gave up and I used a middle man. I just followed this guide and it works very well.

http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

+3
source

Source: https://habr.com/ru/post/952543/


All Articles