My User Entity
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(nullable = false)
private String username;
@Column(nullable = false)
private String password;
@ElementCollection
private List<String> roles = new ArrayList<>();
}
Every User can have many roles. Given a role (represented in String datatype), I want to get all the users that have this role.
For example
User1 with role : "admin"
User2 with role : "user"
User3 with role : "admin"
For the role "admin", I want to get as a result the User1 and User2.
What I have tried with Spring Data Jpa:
public interface UserRepository extends JpaRepository<User, Integer> {
public List<User> findByRoles( String role);
}
But i'm getting an exeption
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.spring.certificatie.securityconfig.User.roles, could not initialize proxy - no Session