4 Replies Latest reply on Sep 16, 2010 5:40 AM by vanz

    Query manytomany

    vanz

      Hi,
      I have a login where you have to enter username, password and role.
      In the database username and password are stored in the same table and role in another one. The relation is manytomany.
      If I had to check only username and password I wouldn't have any problem.
      The problem comes up when I check the three of them together.


      I'm not able to see if the role inserted by the user is correct because all the Query that I wrote failed.
      What is the command and query that allow me to check the three parameters?


      The role inserts by the user is saved into String role while username and password are stored into credentials(variable of Seam).
      Below the user class and role class


      User.java



      package org.domain.docmanager.entity;
      [...] import [...]
      
      @Entity
      @Name("user")
      public class User implements Serializable {
           private static final long serialVersionUID = 1L;
           private Long id;
           private String firstName;
           private String lastName;
           private String username;
           private String password;
           private Set<Role> roles= new HashSet<Role>(0);
           private Set<Workflow> workflow = new HashSet<Workflow>(0);
      
           public User() {
           }
      
           @Id
           @GeneratedValue
           public Long getId() {
                return id;
           }
      
           public void setId(Long id) {
                this.id = id;
           }
      
           @UserPrincipal
           @NotNull
           @Pattern(regex = "^\\w*$")
           public String getUsername() {
                return username;
           }
      
           public void setUsername(String username) {
                this.username = username;
           }
      
           @UserPassword(hash = "none")
           @NotNull
           @Length(min = 5, max = 15)
           @Column(length = 255)
           public String getPassword() {
                return password;
           }
      
           public void setPassword(String password) {
                this.password = password;
           }
           
           @UserRoles
           @ManyToMany(targetEntity = Role.class)
           @JoinTable(name = "UserRole", joinColumns = @JoinColumn(name = "id"), inverseJoinColumns = @JoinColumn(name = "roleId"))
           @DataModel
           public Set<Role> getRoles() {
                return roles;
           }
           
           @Transient
           public List<Role> getRolesList() {
                return new ArrayList<Role>(roles);
           }
      
           public void setRoles(Set<Role> roles) {
                this.roles = roles;
           }
      
           @UserFirstName
           public String getFirstName() {
                return firstName;
           }
      
           public void setFirstName(String firstName) {
                this.firstName = firstName;
           }
      
           @UserLastName
           public String getLastName() {
                return lastName;
           }
      
           public void setLastName(String lastName) {
                this.lastName = lastName;
           }
           
           @OneToMany(mappedBy="user")
           public Set<Workflow> getWorkflow() {
                return workflow;
           }
      
           public void setWorkflow(Set<Workflow> workflow) {
                this.workflow = workflow;
           }
      }





      Role.java



      package org.domain.docmanager.entity;
      
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import org.jboss.seam.annotations.security.management.RoleName;
      
      @Entity
      public class Role {
           private Integer roleId;
           private String rolename;
      
           @Id
           @GeneratedValue
           public Integer getRoleId() {
                return roleId;
           }
      
           public void setRoleId(Integer roleId) {
                this.roleId = roleId;
           }
      
           @RoleName
           public String getRolename() {
                return rolename;
           }
      
           public void setRolename(String rolename) {
                this.rolename = rolename;
           }
      }
       



        • 1. Re: Query manytomany
          kragoth

          What is the error you are getting? It's just a relationship so I'm not sure why it would be difficult.


          Post your query and the error plus stack trace.

          • 2. Re: Query manytomany
            vanz

            I've tried many times and now the problem is logical.
            I want to obtain the roles of a user knowing his username


            When I use the command user.getRoleList().size() I can see clearly the numbers of roles the user has.
            In my case the user can have from 1 to 3 roles (verificatore, approvatore and redattore). If I give a user 3 roles in the database I can see that he has 3 roles when I print it.


            The problem is that I want to see the name of the role (for instance verificatore).
            When I print the information using the command user.getRoleList().toString() I see (org.domain.docmanager.entity.Role@78c4092f, org.domain.docmanager.entity.Role@e372a9a) instead of the roles of the user had used to log in.


            I don't understand where I'm making a mistake.
            Can you help me?
            Thank you.

            • 3. Re: Query manytomany
              sappo

              Hi Andrea,


              your entity class Role doesn't have a toString() method. That means Java uses its default and the default toString() method prints the object's name. In your case org.domain.docmanager.entity.Role@78c4092f and org.domain.docmanager.entity.Role@e372a9a respectively. If you want the rolename to be printed just overwrite the toString() method and return the rolename.


              Greets Kevin

              • 4. Re: Query manytomany
                vanz

                Now it works perfectly!
                Thank you very much!!!