0
votes

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

1

1 Answers

2
votes

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.