I'm developing a Struts2 + Hibernate + Spring application. I'm trying to load the user permissions into my session in order to recover them into the specific jsp.
The workflow is this one:
- The user logs in
- The user group is loaded Eagerly by hibernate
Here is the code from User
class.
@JoinColumn(name = "id_user_group", referencedColumnName = "id", nullable = false)
@ManyToOne(optional = false, fetch = FetchType.LAZY, targetEntity = UserGroup.class)
public UserGroup getUserGroup() {
return userGroup;
}
- From the User group i recover another EAGER relation
Here is the code from UserGroup
class
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "user_section_permission", joinColumns = {
@JoinColumn(name = "id_user_group", nullable = false, updatable = false)},
inverseJoinColumns = {
@JoinColumn(name = "id_section_permission",
nullable = false, updatable = false)})
public List<SectionPermission> getSectionPermissions() {
return sectionPermissions;
}
- The user is saved into the session. When the login action redirects the user to his dashboard then a
refreshPermissions
method is called
Here is the code:
UserGroup userGroup = ((User) session.get("user")).getUserGroup();
Map<String, Section> permissions = new HashMap<>();
List<SectionPermission> sectionPermissions = userGroup.getSectionPermissions();
for (SectionPermission sectionPermission : sectionPermissions) {
permissions.put(sectionPermission.getCode(), sectionPermission.getSection());
}
session.put("permissions", permissions);
I think it's easy to understand that I simply create a map with the permission code and his relative section (in order to populate the menu and so on.
And now the problem... My tomcat shows the permission attribute as empty
But magically if I add a LOG.debug("Perm: {}", new Object[]{sectionPermission.getCode(), sectionPermission.getSection()});
inside the for loop then the permissions appear...
What's happening? Am I doing something wrong with Hibernate or Spring?