0

I'm facing a problem that I can't solve. I have 2 DTOs, one has a collection attribute to another DTO.

public class UnidadAsistencialDTO 

private Long id;
private String descripcion;
@JsonManagedReference
private Collection<ServicioDTO> servicios;


public class ServicioDTO 

private Long id;
private String nombre;
private Collection<EspecialidadMinDTO> especialidades;
@JsonBackReference
private Collection<UnidadAsistencialDTO> unidadesAsistenciales;

So when I try to obtain these objects I have this exception

11:52:46,708 WARN [org.jboss.resteasy.core.SynchronousDispatcher] (http-localhost/127.0.0.1:8080-1) Failed executing POST /agenda: org.jboss.resteasy.spi.ReaderException: org.codehaus.jackson.map.JsonMappingException: Can not handle managed/back reference 'defaultReference': back reference type (java.util.Collection) not compatible with managed type (ar.org.inssjp.asthenis.dto.agenda.UnidadAsistencialDTO) at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:202) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:136) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:159) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:269) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:227) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:216) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:542) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:524) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:126) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:231) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19] at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19] at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:926) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_71] Caused by: org.codehaus.jackson.map.JsonMappingException: Can not handle managed/back reference 'defaultReference': back reference type (java.util.Collection) not compatible with managed type (ar.org.inssjp.asthenis.dto.agenda.UnidadAsistencialDTO) at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:315) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:290) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:159) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:620) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.BeanDeserializer.resolve(BeanDeserializer.java:379) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._resolveDeserializer(StdDeserializerProvider.java:407) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:352) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:290) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:159) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider.findTypedValueDeserializer(StdDeserializerProvider.java:180) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.ObjectMapper._findRootDeserializer(ObjectMapper.java:2829) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2699) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419) at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:106) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.read(GZIPDecodingInterceptor.java:63) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:109) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:169) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] ... 27 more Caused by: java.lang.IllegalArgumentException: Can not handle managed/back reference 'defaultReference': back reference type (java.util.Collection) not compatible with managed type (ar.org.inssjp.asthenis.dto.agenda.UnidadAsistencialDTO) at org.codehaus.jackson.map.deser.BeanDeserializer._resolveManagedReferenceProperty(BeanDeserializer.java:497) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.BeanDeserializer.resolve(BeanDeserializer.java:382) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._resolveDeserializer(StdDeserializerProvider.java:407) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:352) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:290) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:159) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:620) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.BeanDeserializer.resolve(BeanDeserializer.java:379) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._resolveDeserializer(StdDeserializerProvider.java:407) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:352) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:290) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:159) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:620) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.BeanDeserializer.resolve(BeanDeserializer.java:379) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._resolveDeserializer(StdDeserializerProvider.java:407) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:352) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:290) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:159) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.BasicDeserializerFactory.createCollectionDeserializer(BasicDeserializerFactory.java:283) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createDeserializer(StdDeserializerProvider.java:389) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:310) [jackson-mapper-asl.jar:1.9.13] ... 44 more

I was reading in some blog, that these annotations "@JsonManagedReference" and "@JsonBackReference" don't fixed the problem when you have Collections on both side (like a Many to Many relationship).

Can you help me please?

Thanks

Franco Cerati
  • 21
  • 1
  • 2
  • 3

1 Answers1

2

Try adding this annotations to the classes:

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = UnidadAsistencialDTO.class))

And

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = ServicioDTO.class))

And also take a look at this for further reference.

EDIT

If this doesn't work (as in your case) try this instead:

@JsonIgnore

Docs of this are here.

UPDATE

If you need to have a parent with their childs list AND the child with a reference to the parent, I suggest you go either way of:

  1. Delete the reference of the parent in the childs when calling the parent, and delete the reference of the childs if calling the childs (just in-memory and right-before transforming the DTO's to json). You'll end up with something like this:

    { "name":"theParent", "childs":[ { "name":"child1", "parent":null },{ "name":"child2", "parent":null } ] }

    Nothe the null in the reference to the parent from the childs. Also, as a tip, make sure you don't delete the parent reference INSIDE a transaction context.

  2. If having a null reference inside your child (or parent, depending what you're quering) isn't correct, you can make a new set of DTOs that map to the structure you're looking for. If you go for this, take a look at this for the right way to do it.

Community
  • 1
  • 1
chomsky
  • 300
  • 3
  • 13
  • One more questions, those annotations that you have mentioned where I can find them? I mean, which library should I add in my POM file? I'm using Jboss EAP. – Franco Cerati Feb 18 '15 at 18:01
  • [Here's](http://fasterxml.github.io/jackson-annotations/javadoc/2.0.0/com/fasterxml/jackson/annotation/JsonIdentityInfo.html) the link of the API, and [here's](http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations/2.5.1) the link to the maven dependency – chomsky Feb 18 '15 at 18:40
  • I added the annotations the you have told me, and now I have another exception. – Franco Cerati Feb 18 '15 at 21:37
  • 18:35:01,596 WARN [org.jboss.modules] (http-localhost/127.0.0.1:8080-4) Failed to define class org.codehaus.jackson.map.JsonMappingException$Reference in Module "org.codehaus.jackson.jackson-mapper-asl:main" from local module loader 515c9cc0 (finder: local module finder 17372afb (roots: C:\Users\\workspace\jboss-eap-6.3\modules,C:\Users\\workspace\jboss-eap-6.3\modules\system\layers\base)): java.lang.StackOverflowError at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) – Franco Cerati Feb 18 '15 at 21:42
  • Kind of that was my only known trick for that. But if you share the new error we can keep finding out whats wrong – chomsky Feb 18 '15 at 21:42
  • ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/asthenis-rest].[ar.org.inssjp.asthenis.core.JaxRsActivator]] (http-localhost) JBWEB000236: Servlet.service() for servlet ar.org.inssjp.asthenis.core.JaxRsActivator threw exception: java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:392) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.jboss.resteasy.plugins.server.servlet.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:109) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] – Franco Cerati Feb 18 '15 at 21:42
  • I was using @JsonIgnore annotation but the problem was that I want my object completed with information or data in the attributes, so in this case in particular is not what I need. I don't know if I explain really good... – Franco Cerati Feb 18 '15 at 22:44
  • Yes you do. What i mean for this solution is that the child object have this annotation to stop returning to the father. If you ALSO whant to query the son and reference the father... that would be tricky – chomsky Feb 18 '15 at 22:57
  • Yes of course, so how can I do to solve that problem? I'm thinking, if want to know all ServicioDTO that I have in UnidadAsistencialDTO I don't have problem adding this annotation @JsonIgnore to the son. But if I add this annotation and I want to know all UnidadAsistencialDTO that ServicioDTO has, I can do it with this annotation because they will give me an NULL. Correct? So in this case, if I am not wrong, I 'm facing this big problem... – Franco Cerati Feb 19 '15 at 00:26
  • I found some info [here](http://stackoverflow.com/questions/26698434/jackson-deserialization-fails-on-circular-dependencies/26700267#26700267) and [here](http://stackoverflow.com/questions/26657259/hibernate-and-json-is-there-a-definitive-solution-to-circular-dependencies). Hope this guide you to the solution. Let us know how it goes – chomsky Feb 19 '15 at 06:47
  • I have seen those post that you told me and I have tried with the solution but I didn't solve my problem. I think that the big problem in my case is: In my parent object I have a collection of my childrens. In my children objects I have a collection of my parents. What I im trying to say is that when we have this "many to many" relation between object, those annotation don't work. – Franco Cerati Feb 19 '15 at 13:46
  • Sorry, also I want to say that I can not use annotations of FasterXML package because I don't know why they dont function. So I'm using annotation of "jackson-mapper-asl". My AS is a Jboss EAP 6.3.0 with api of resteasy "jboss-jaxrs-api_1.1_spec-1.0.1.Final-redhat-2" – Franco Cerati Feb 19 '15 at 13:49