I have an Entity that i can persist into the Database.
import jakarta.persistence.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
import java.util.Set;
@Entity
@Table(name = "users")
public class UserModel implements UserDetails {
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private long id;
@Column(nullable = false, unique = true)
private String username;
private String password;
@ElementCollection()
private Collection<SimpleGrantedAuthority> grantedAuthorities;
private boolean isAccountNonExpired;
private boolean isAccountNonLocked;
private boolean isCredentialsNonExpired;
private boolean isEnabled;
public UserModel() {
}
public UserModel(String username, String password, Collection<SimpleGrantedAuthority> grantedAuthorities, boolean isAccountNonExpired, boolean isAccountNonLocked, boolean isCredentialsNonExpired, boolean isEnabled) {
this.username = username;
this.password = password;
this.grantedAuthorities = grantedAuthorities;
this.isAccountNonExpired = isAccountNonExpired;
this.isAccountNonLocked = isAccountNonLocked;
this.isCredentialsNonExpired = isCredentialsNonExpired;
this.isEnabled = isEnabled;
}
@Override
public Collection<? extends SimpleGrantedAuthority> getAuthorities() {
return grantedAuthorities;
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return username;
}
@Override
public boolean isAccountNonExpired() {
return isAccountNonExpired;
}
@Override
public boolean isAccountNonLocked() {
return isAccountNonLocked;
}
@Override
public boolean isCredentialsNonExpired() {
return isCredentialsNonExpired;
}
@Override
public boolean isEnabled() {
return isEnabled;
}
@Override
public String toString() {
return "UserModel{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", grantedAuthorities=" + grantedAuthorities +
", isAccountNonExpired=" + isAccountNonExpired +
", isAccountNonLocked=" + isAccountNonLocked +
", isCredentialsNonExpired=" + isCredentialsNonExpired +
", isEnabled=" + isEnabled +
'}';
}
}
The problem with the code above is that since i have a HashSet for my enumerated values, i wanted to be able to store them connected with my User. I abandoned @OneToMany relationship annotation in favor of working with Sets, as it seems to have been limited considering i'm overriding default implementations.
My question however is mainly: How do i write a QUERY for my persisted data that is a Hashset of Strings, inside of my repository interface which finds and returns a UserModel's grantedAuthorities?
I have the following data persisted: PGAdmin 4 structure
Authorities with 3 values stored separetely, for some reason
I looked at: JPA @ElementCollection how can I query? to try and see if i could replicate, but wasn't queried properly for me.
I'm expecting to fetch a UserModels grantedAuthorities ID or related authorities.
UPDATE: My repository query:
@Query("select u from UserModel u where u.grantedAuthorities = ?1")
Set<GrantedAuthority> findUserModelsAuthorities(String id);
ERROR:
Argument [512] of type [java.lang.String] did not match parameter type [org.springframework.security.core.authority.SimpleGrantedAuthority (n/a)]