Hi Håkon,
is the role_id property in the Role entity annotated with @Id or in any other way considered an ID by OpenJPA?

Are you adding roles to new users creating new instances of the Role classes (like User u = new User(); u.addRole(new Role(Roles.ADMIN)); .. ) or getting preexisting Role entities from the database?

If you are creating new Role(..) every time, OpenJPA properly supposes that is a new row to add to the table.

Hope this helps,
Simone

Håkon Sagehaug wrote:
Hi

I've got the following setup. A user entity and a role entity. The user has
a collection of Role, annotated like this


public class User{

        ....
       @ManyToMany(cascade = CascadeType.ALL)
         private Collection<Role> roles = new HashSet<Role>();

}

public Role {
       private int role_id;

    @Column(name = "role")
    private Roles role;
}

Where Roles is a enum, with the possible roles. So first of all is this the
correct setup if I also want to have many users been able to have the same
role?

e.g User 1, Role:Admin,User,Tester
       User2, Role:Admin,Tester

So when persisting a new user the role is also persists the Role object even
if there is one from before. so in the example above the Admin and tester
role would be inserted twice, and I of course want them to be inserted just
once. Is there a way to do this? Should  annotate the Role entity with
something more?

cheers, Håkon




--
Simone Gianni            CEO Semeru s.r.l.           Apache Committer
http://www.simonegianni.it/

Reply via email to