JPA many-to-many mapping problem, please help

Discussions

EJB programming & troubleshooting: JPA many-to-many mapping problem, please help

  1. JPA many-to-many mapping problem, please help (1 messages)

    Hi,

    I am using Eclipselink to model the following relationship:

    User (many to many) Role

    I also have one table called "Attribute", which contain a USER_ID and ROLE_ID column

    Therefore for one "Attribute" object, it will reference to one User and one Role

    I can successfully map the many to many between User and Role, but don't know how to map the Attribute entity

    I try the following but the mapping result is wrong:

    =========================

    class Attribute()

    @ManyToOne

    @JoinColumn(name="USER_ID")

    User user;

    @ManyToOne

    @JoinColumn(name="ROLE_ID")

    Role role;

    =========================

    How can I specify the User and Role relationship?

    Thanks

  2. While implementing Many to Many relationships, say your User class might have 3 (or more) associated Roles for single User Object (and vice-versa), so we need Collections to store those 3 associated Role Objects. Hence the classes must contain following collection methods for fulfilling Many-to-Many relationship

    • Class User must contain (besides user_id and other ):

    @ManyToMany@JoinTable(name="Attribute")

    Collection<Role> roles;

    Similarly Class Role must also contain (besided role_id & other methods)

    Collection<User> users;

    I guess specifying Attribute class is not required at all.

    Regards

    Sarabjit S. Rupaal