0

The following tables were defined for Spring Security. I want to fetch all users and all their associated roles. The table has entry for only one user and two roles (employee and admin). However, hibernate fetches two values for AuthorityDO both "Admin". Please help.

users table:

CREATE TABLE users (
  `username` varchar(50) NOT NULL,
  `password` char(68) NOT NULL,
  `enabled` tinyint(1) NOT NULL,
  PRIMARY KEY (`username`)
);

authorities table:

CREATE TABLE authorities (
  `username` varchar(50) NOT NULL,
  `authority` varchar(50) NOT NULL,
  UNIQUE(username,authority),
  FOREIGN KEY (username) REFERENCES users(username)
);

Users Entity class:

@Entity
@Table(name="users")
public class UserDO {
    @Id
    @Column(name = "username")
    String username;
    @Column(name = "password")
    String password;
    @Column(name = "enabled")
    boolean enabled;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "userDO")
    List<AuthorityDO> authorities;
    public UserDO() {
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public boolean isEnabled() {
        return enabled;
    }
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
    public List<AuthorityDO> getAuthorities() {
        return authorities;
    }
    public void setAuthorities(List<AuthorityDO> authorities) {
        this.authorities = authorities;
    }
}

Authorities Entity class:

@Entity
@Table(name = "authorities", uniqueConstraints = {@UniqueConstraint(columnNames = {"username", "authority"})})
public class AuthorityDO {
    @Id
    @Column(name = "username")
    String username;
    @Column(name = "authority")
    String authority;
    @ManyToOne
    @JoinColumn(name = "username", referencedColumnName = "username", insertable = false, updatable = false)
    UserDO userDO;
    public AuthorityDO() {
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getAuthority() {
        return authority;
    }
    public void setAuthority(String authority) {
        this.authority = authority;
    }
    public UserDO getUserDO() {
        return userDO;
    }
    public void setUserDO(UserDO userDO) {
        this.userDO = userDO;
    }
}

Code

public List<UserDO> getUsers() {
        Session currentSession = factory.getCurrentSession();
        Query<UserDO> query = currentSession.createQuery("from UserDO");
        List<UserDO> customers = query.getResultList();
        return customers;
    }
Anonymous
  • 123
  • 1
  • 4

1 Answers1

0

I found the issue. I was mapping composite key incorrectly on authorities. That solved the problem.

Code changes:

@Entity
@Table(name = "authorities", uniqueConstraints = { @UniqueConstraint(columnNames = { "username", "authority" }) })
public class AuthorityDO {
    @EmbeddedId
    private AuthorityDOCompositeKey compositeKey;
    @ManyToOne
    @JoinColumn(name = "username", referencedColumnName = "username", insertable = false, updatable = false)
    UserDO userDO;

    public AuthorityDO() {
    }

    public AuthorityDOCompositeKey getCompositeKey() {
        return compositeKey;
    }

    public void setCompositeKey(AuthorityDOCompositeKey compositeKey) {
        this.compositeKey = compositeKey;
    }

    public UserDO getUserDO() {
        return userDO;
    }

    public void setUserDO(UserDO userDO) {
        this.userDO = userDO;
    }
}

Composite Key class:

@Embeddable
public class AuthorityDOCompositeKey implements Serializable {
    @Column(name = "username")
    String username;
    @Column(name = "authority")
    String authority;

    public AuthorityDOCompositeKey() {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAuthority() {
        return authority;
    }

    public void setAuthority(String authority) {
        this.authority = authority;
    }
}

All thanks to this question: @OneToMany and composite primary keys?

Now I'll try to set it as uni directional

Anonymous
  • 123
  • 1
  • 4