Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

many to many relation with attributes using annotations

enter image description here

I want to know how to create the table role_permission with the annotation @ManyToMany taking into account the attribute created_at in the table role_permission.

I know that 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 to create a new class for role_permission, but I dont know how to put created_at in this annotation. Is this possible?

like image 375
kiduxa Avatar asked Aug 26 '13 21:08

kiduxa


1 Answers

I gave up and I used the intermediary. I just followed this tutorial and it works pretty well.

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

like image 192
kiduxa Avatar answered Nov 16 '22 01:11

kiduxa