[resteasy-dev] JSON-B and lazy JPA entity
Tomaž Cerar
tomaz.cerar at gmail.com
Fri Apr 5 08:54:11 EDT 2019
What is your UserDao in this case?
if you would be in transaction it should still work.
Does it work if you add @Transactional on your rest method?
--
tomaz
On Fri, Apr 5, 2019 at 2:39 PM Marek Kopecky <mkopecky at redhat.com> wrote:
> 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:
>
> 1. Do not return entity, use specific DTO
> 2. Iterate all collections in rest end-point (for example [2])
> 3. Use Jackson + jackson-datatype-hibernate module +
> ContextResolver<ObjectMapper> implementation [3]. This hibernate module is
> not a part of WF (so this is probably not recommended), but it can be added
> to the deployment. This approach doesn't throw LazyInitializationException,
> but this doesn't return lazy collections as well.
> 4. Use JsonbTransient annotation. This approach doesn't throw
> LazyInitializationException, but this doesn't return lazy collections as
> well.
> 5. Convert object to string json in end-point (something like [5]).
> End-point returns String.
> 6. ??? Some other workaround ???
>
> 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));
> }
> _______________________________________________
> resteasy-dev mailing list
> resteasy-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/resteasy-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/resteasy-dev/attachments/20190405/0100906c/attachment.html
More information about the resteasy-dev
mailing list