I am using Java 1.7. I have a REST service, when I call it, I get the following error when it tries to map the Java object to JSON:
HTTP Status 500 - org.codehaus.jackson.map.JsonMappingException: failed to lazily initialize a collection of role: entity.Person.flightPreferences, no session or session was closed (through reference chain: com.nexct.auth.model.UserDetailsAndRoles["person"]->entity.Person["approver"]->entity.approval.AutoApprovalApproverConfiguration["approver"]->entity.Person["flightPreferences"])
and
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: entity.Person.flightPreferences, no session or session was closed
Code:
Person.java
@OneToMany(cascade = CascadeType.ALL)
@JsonIgnore
public List<TravellerFlightPreference> getFlightPreferences()
{
return flightPreferences;
}
This post suggested to add @JsonIgnore
, but I still get the error.
AuthenticationResource.java
@GET
@Path("/details")
@ApiOperation(value="Get user details for a user held in the jwt")
@ApiResponses({
@ApiResponse(code=200, message="Success"),
@ApiResponse(code=404, message="Not Found")
})
@Produces({ MediaType.APPLICATION_JSON })
public Response details(@HeaderParam("Authorization") String token, @Context HttpServletRequest request) {
UserDetailsAndRoles userDetailsAndRoles = new UserDetailsAndRoles();
String jwtToken = getJwtToken(token);
String userName = jwtTokenUtil.extractUserName(jwtToken);
Set<Role> memberRoles = new HashSet<>();
Set<UserRole> userRoles = new HashSet<>();
try {
Member member = memberService.getMemberByUserName(userName);
Person person = personService.getPersonEntityByMemberId(member.getId());
//List<TravellerFlightPreference> travellerFlightPreferences = person.getFlightPreferences();
List<Employee> employees = employeeService.findEmployeeByMemberId(member.getId());
Contact contact = loginLocal.findContactMemberId(member.getId());
List<Account> accounts = loginLocal.findAllAccountByMemberId(member.getId());
memberRoles = roleManager.viewRolesAssignedToMember(new ViewRolesAssignedToMemberRequest(member.getId())).getRoles();
for (Role role: memberRoles) {
UserRole userRole = new UserRole();
userRole.setId(role.getId());
userRole.setName(role.getName());
userRole.setDescription(role.getDescription());
userRoles.add(userRole);
}
userDetailsAndRoles.setAccounts(accounts);
userDetailsAndRoles.setContact(contact);
userDetailsAndRoles.setEmployees(employees);
userDetailsAndRoles.setMember(member);
userDetailsAndRoles.setPerson(person);
userDetailsAndRoles.setUserRoles(userRoles);
//userDetailsAndRoles.setTravellerFlightPreferences(travellerFlightPreferences);
} catch (MemberDoesNotExistException e) {
logger.severe("Error getting member for userName: "+userName);
e.printStackTrace();
return Response.ok(null, "Error getting member for userName: "+userName+". "+e.getMessage()).build();
}
return Response.ok().entity(userDetailsAndRoles).build();
}
I think this error is because the flightPreferences
are not loaded when the Person
object is converted to JSON.
Question
- In the
Person
object, I do not tell Hibernate to Laizily load theflightPreferences
, why are they not available? - When I add the
@JsonIgnore
, why does it still get the error?