<div dir="ltr"><div>What is your UserDao in this case?</div><div><br></div><div>if you would be in transaction it should still work.</div><div>Does it work if you add @Transactional on your rest method?</div><div><br></div><div>--</div><div>tomaz<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Apr 5, 2019 at 2:39 PM Marek Kopecky &lt;<a href="mailto:mkopecky@redhat.com">mkopecky@redhat.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
  

    
  
  <div bgcolor="#FFFFFF">
    <p>Hi,</p>
    <p>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 &amp;&amp; LazyInitializationException.</p>
    <p>AFAIK, there are several workarounds, like:</p>
    <ol>
      <li>Do not return entity, use specific DTO</li>
      <li>Iterate all collections in rest end-point (for example [2])<br>
      </li>
      <li>Use Jackson + jackson-datatype-hibernate module +
        ContextResolver&lt;ObjectMapper&gt; 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&#39;t throw LazyInitializationException, but this
        doesn&#39;t return lazy collections as well.</li>
      <li>Use JsonbTransient annotation. This approach doesn&#39;t throw
        LazyInitializationException, but this doesn&#39;t return lazy
        collections as well.</li>
      <li>Convert object to string json in end-point (something like
        [5]). End-point returns String.</li>
      <li>??? Some other workaround ???<br>
      </li>
    </ol>
    <p>Are there some correct solution or plan to implement correct
      solution? If not, which workaround is recommended?<br>
    </p>
    <p>Marek</p>
    <p>========================================</p>
    <p>[1] </p>
    <p>@Entity<br>
      public class User {<br>
          @OneToMany(mappedBy = &quot;owner&quot;, fetch = FetchType.LAZY)<br>
          List&lt;Task&gt; tasks;<br>
          ...</p>
    <p>}</p>
    <p>    @GET<br>
          @Path(&quot;get/normal/{id}&quot;)<br>
          @Produces(MediaType.APPLICATION_JSON)<br>
          public User getByIdNormal(@PathParam(&quot;id&quot;) Long id) throws
      Exception {<br>
              return userDao.getUser(id);<br>
          }</p>
    <p>========================================<br>
    </p>
    <p>[2] <br>
    </p>
    <p>    @GET<br>
          @Path(&quot;get/iterate/{id}&quot;)<br>
          @Produces(MediaType.APPLICATION_JSON)<br>
          public User getByIdIterate(@PathParam(&quot;id&quot;) Long id) throws
      Exception {<br>
              return fixLazy(userDao.getUser(id));<br>
          }<br>
      <br>
          private User fixLazy(User u) {<br>
              Iterator&lt;Task&gt; it = u.getTasks().iterator();<br>
              while (it.hasNext()) {<br>
                  it.next();<br>
              }<br>
              return u;<br>
          }</p>
    <p>========================================</p>
    <p>[3]</p>
    <p>@Provider<br>
      @Produces(MediaType.APPLICATION_JSON)<br>
      public class JacksonDatatypeJacksonProducer implements
      ContextResolver&lt;ObjectMapper&gt; {<br>
          private final ObjectMapper json;<br>
          public JacksonDatatypeJacksonProducer() throws Exception {<br>
              this.json = new ObjectMapper()<br>
                      .findAndRegisterModules()<br>
                      .registerModule(new Hibernate5Module());<br>
          }<br>
          @Override<br>
          public ObjectMapper getContext(Class&lt;?&gt; objectType) {<br>
              return json;<br>
          }<br>
      }</p>
    <p>========================================</p>
    <p>[5]</p>
    <p>    @GET<br>
          @Path(&quot;get/iterate/{id}&quot;)<br>
          public String getByIdConvertToString(@PathParam(&quot;id&quot;) Long id)
      throws Exception {<br>
              return convertObjectToJsonString(userDao.getUser(id));<br>
          }<br>
    </p>
  </div>

_______________________________________________<br>
resteasy-dev mailing list<br>
<a href="mailto:resteasy-dev@lists.jboss.org" target="_blank">resteasy-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/resteasy-dev" rel="noreferrer" target="_blank">https://lists.jboss.org/mailman/listinfo/resteasy-dev</a><br>
</blockquote></div>