0

There is a @ManyToMany table: User, user_role, roleuser.

enter image description here

The roleuser table contains roles: ROLE_USER, ROLE_ADMIN and others.

This is how I make the @ManyToMany tables relate to each other:

User.class

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    public List<Role> getRoleList() {
        return roleList;
    }

Role.class

    @ManyToMany(mappedBy = "roleList", cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    public List<User> getUserList() {
        return userList;
    }    



    public void addPerson(User user) {
        userList.add( user );
        user.getRoleList().add( this );
    }

And this is how I add the user:

AddUser.class

    User user = new User("Michael Joseph Jackson");

    Role role = serviceJpa.findRoleByRole("ROLE_USER");
    role.addPerson(user);

    serviceJpa.saveRole(role);

The bottom line is that I don't want to use fetch = FetchType.EAGER. One role can have thousands of users, and I don't want to get all thousand users when I access the role. But to do this, I have to change FetchType.EAGER to FetchType.LAZY, but then in this case, a well-known error occurs when I add a user:

Message Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.test.shop.model.user.Role.userList, could not initialize proxy - no Session

I don’t know what to do about it. Please tell me what am I doing wrong?

4
  • You need to also assign to the user a list of roles. Try doing this. Commented Jul 5, 2021 at 4:50
  • How should I assign a list of roles? I have a role and I am assigning this role to a new user. How else should I assign a list of roles to a user? Can you show? Commented Jul 5, 2021 at 5:14
  • 2
    why should you have relation from role to user ? and why should role have those cascades on user ? but you might be able to solve your problem with adding @Transactional on the methods that you are using User or Role. Commented Jul 5, 2021 at 5:15
  • @AliDahaghin You speak: why should you have relation from role to user ? Otherwise, my user is not added. Commented Jul 5, 2021 at 7:27

1 Answer 1

0

I thinking your code is a bad pattern, when you call serviceJpa.saveRole(role); jpa first loading roll.userList and adding user into it, then saving it's relation. therefore , jpa first load roll.userList, but you set roll.userList load as lazy so jpa can not load userList beacuse your session closed!!! please read this tutorial

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.