Hi,
what is the recommended way (best practice) to handle returning the JPA entity with lazy loading from REST end-point with json-b? Example [1] ends with 500 && LazyInitializationException.
AFAIK, there are several workarounds, like:
Are there some correct solution or plan to implement correct
solution? If not, which workaround is recommended?
Marek
========================================
[1]
@Entity
public class User {
@OneToMany(mappedBy = "owner", fetch = FetchType.LAZY)
List<Task> tasks;
...
}
@GET
@Path("get/normal/{id}")
@Produces(MediaType.APPLICATION_JSON)
public User getByIdNormal(@PathParam("id") Long id) throws
Exception {
return userDao.getUser(id);
}
========================================
[2]
@GET
@Path("get/iterate/{id}")
@Produces(MediaType.APPLICATION_JSON)
public User getByIdIterate(@PathParam("id") Long id) throws
Exception {
return fixLazy(userDao.getUser(id));
}
private User fixLazy(User u) {
Iterator<Task> it = u.getTasks().iterator();
while (it.hasNext()) {
it.next();
}
return u;
}
========================================
[3]
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonDatatypeJacksonProducer implements
ContextResolver<ObjectMapper> {
private final ObjectMapper json;
public JacksonDatatypeJacksonProducer() throws Exception {
this.json = new ObjectMapper()
.findAndRegisterModules()
.registerModule(new Hibernate5Module());
}
@Override
public ObjectMapper getContext(Class<?> objectType) {
return json;
}
}
========================================
[5]
@GET
@Path("get/iterate/{id}")
public String getByIdConvertToString(@PathParam("id") Long id)
throws Exception {
return convertObjectToJsonString(userDao.getUser(id));
}