9

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?

kiduxa
  • 3,339
  • 11
  • 37
  • 52
  • http://stackoverflow.com/questions/10294338/many-to-many-hibernate-mapping-for-additional-property-in-the-join-table/10301484#10301484 http://stackoverflow.com/questions/4751902/adding-additional-property-to-hibernate-jointable – Luca Basso Ricci Aug 26 '13 at 21:35
  • 1
    the first link use an intermediary and I want to avoid this and the second is not very clear and uses a hashmap, what if I want more attributes in the table role_permission?. Are there any other options? – kiduxa Aug 26 '13 at 21:48
  • None that I know. If someone knows alternative solutions will answer/comment. IMHO, sometimes, the best solution is the one I can understand, manage and modify without trouble – Luca Basso Ricci Aug 26 '13 at 21:52

1 Answers1

6

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/

kiduxa
  • 3,339
  • 11
  • 37
  • 52
  • Thanks! ....holy komoly, that was so much work trying to create the mapping using JPA annotations to the many to many join table with extra attributes. this after maybe 5 other examples. interesting, i was so hard for a 13K Java Class Oil Business Invoicing App, that they moved/denormalized the attributes out of the many to many table to the two ref. tables in the DB. Unreal.... – tom Nov 13 '18 at 17:32