1

With or without this annotation, there is a property on my JPA @Entity

@Entity
public class Myentity extends ResourceSupport implements Serializable {
...
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="idrepository")
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private MyentitySource entitysource;
...
}

that is not being mapped when I return:

@RequestMapping("/myentity/{uuid}")
public ResponseEntity<Myentity> getResourceById(@PathVariable("uuid") UUID uuid) {
    Myentity result = myentityRepository.findOne(uuid);
    return ResponseEntity.ok(myentityAssembler.toResource(result));
}

myentityAssembler.toResource(result) does contain this MyentitySource entitysource, but the JSON output does not.

The weirdest thing is I have another spring boot hateoas project where I am using the exact same entity, repository, controller, and assembler implementations, with the exact same dependencies and versions on my pom, and a very similar configuration (I am not defining any special jackson mappers or anything, just using the default rest/hateoas configuration), and it does work there: The MyentitySource entitysource property, which is another JPA entity extending ResourceSupport, gets serialized and included into the JSON output.

I have been a couple of hours at it already, but I am quite lost. I have verified this behavior is happening all through the application in both applications: @ManyToOne relations defined on any @Entity are being mapped and present in the JSON output on one application, but not in the other.

How can I get these fields to show up on the JSON output?

a better oliver
  • 26,330
  • 2
  • 58
  • 66
NotGaeL
  • 8,344
  • 5
  • 40
  • 70
  • One thing is different when I try to reproduce your issue. I have to use `@JsonInclude(value=Include.ALWAYS)` instead of `@JsonInclude(JsonInclude.Include.ALWAYS)`. Dont think its the issue but you can have a try. – Patrick Sep 08 '16 at 06:04
  • no difference. Using Eclipse debugger I can see there is a `org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration$ResourceSupportHttpMessageConverter` whose `ObjectMapper` has a `BeanSerializerFactory` which has a `SerializerFactoryConfig` which has a `org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$AssociationOmittingSerializerModifier` (which is a `BeanSerializerModifier`), and I believe that modifier is messing with my JPA `@ManyToOne` association. – NotGaeL Sep 08 '16 at 08:31
  • I will try now to write my own `RepositoryRestMvcConfiguration` and override or modify the converter to use an `ObjectMapper` whose serializer factory config doesn't include this modifier. – NotGaeL Sep 08 '16 at 08:31

1 Answers1

1

entitysource will be included if MyentitySource is not an exported entity. If it is one - what seems to be the case here - then it would be wrong to include it. Including associations could lead to sending the whole database to the client. Moreover it is a separate resource with its own URI. Consequently a link to that URI is included in the response.

CascadeType.ALL implies that Myentity is an aggregate, therefore MyentitySource should not be exported in the first place. That would solve your problem. If my assumption is wrong, then you can still use Projections to get entitysource included. I can refer you to this answer from Spring's Oliver Gierke and the relevant chapter of the documentation.

Community
  • 1
  • 1
a better oliver
  • 26,330
  • 2
  • 58
  • 66
  • I've tried exporting MyentitySource (I mean, defining a repository for it), which according to the docs should make the mapper include the resource url but it doesn't. I've also tried not exporting it (no repo), which should include its properties (all but the id). I have the entities defined on this project.There are basically a copy paste from a different project where they are working fine,the same way the docs say they will. Why do they work one way on one of my projects and another way on the other? I haven't defined any Projections or done anything special to change the default behavior – NotGaeL Sep 08 '16 at 09:38
  • BTW, thanks for linking the docs. I tried to find that on google and spring boot docs but couldn't. At least now I know how it's supposed to work ^^ – NotGaeL Sep 08 '16 at 09:40
  • Also, if I understood the concept definition, Myentity is an aggregate of MyentitySource. MyentitySource is a property of Myentity (like an associated category for an item). In any case, there is no way the whole database is going to be included in this particular case, although I understand the danger, and it might not be very the most efficient to repeat the same embedded resource when query results in multiple Myentity items if they share it so embedding its URI instead when MyentitySource is also exported as a hateoas resource might be a better solution. But that isn't happening either... – NotGaeL Sep 08 '16 at 09:54
  • Correction: removing the repo *did* solve the issue (I must have done my first test wrong). I still don't know why the link is not been added instead when a repo for the linked entity is available, as the documentation specifies, but this solution works for me. Thank you :-) – NotGaeL Sep 08 '16 at 13:46