ManyToOne referenced entities not exist in JSON Response when returning PagedResource

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


ManyToOne referenced entities not exist in JSON Response when returning PagedResource



When I return a Page<Entity> from a method inside my @RestController class, all fields of Entity both referenced via @OneToXXX and @ManyToXXX take place in the returned JSON object. But when I switched the return type to PagedResource (to be able to add links to the response), @ManyToXXX fields are not included at all.


Page<Entity>


@RestController


Entity


@OneToXXX


@ManyToXXX


PagedResource


@ManyToXXX



Here is the method in question:


@GetMapping("/fetch")
public PagedResources getResults(Pageable pageable, PagedResourcesAssembler assembler) {

Page<ParentClass> page = myRepository.findAll(pageable);

PagedResources pagedResources = assembler.toResource(page, myResourceAssembler);

return pagedResources;
}



Here are the basic class definitions:



ParentClass


@Entity
@Table(name = "parent_table", catalog = "myDB")
public class ParentClass implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;

@ManyToOne(optional = false)
@JoinColumn(name = "other_class", referencedColumnName = "id")
private OtherClass otherClass;

@OneToOne(mappedBy = "parent")
private SampleField1 sampleField1;

@OneToMany(mappedBy = "parent")
private List<SampleField2> sampleField2;

}



SampleField1 OneToXXX


@Entity
@Table(name = "sample_table_1", catalog = "myDB")
public class SampleField1 implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;

@Column(name="some_field")
String someField;

@OneToOne
@JoinColumn(name = "sample_field_1", referencedColumnName = "id")
@JsonBackReference //to avoid infinite recursion
private ParentClass parent;
}



OtherClass ManyToOne


@Entity
@Table(name = "other_table", catalog = "myDB")
public class OtherClass implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer id;

@Column(name="some_other_field")
String someOtherField;

// I don't need any reference to ParentClass here.
}



To add further detail to the issue here is the logging output of changeProperties() method inside PersistentEntityJackson2Module class:


changeProperties()


PersistentEntityJackson2Module


s.d.r.w.j.PersistentEntityJackson2Module : Assigning nested entity serializer for @javax.persistence.OneToOne(..) com.project.SampleField1 com.project.model.ParentClass.sampleField1
s.d.r.w.j.PersistentEntityJackson2Module : Assigning nested entity serializer for @javax.persistence.OneToMany(..) com.project.SampleField2 com.project.model.ParentClass.sampleField2
// .... omitted other lines for brevity



the resulting JSON is :


{
"_embedded":{
"parentClasses":[
{
"id":1,
// <-- There is no field for otherClass !
"sampleField1":{
"id":1,
"sampleField":"blabla"
},
"sampleField2":[ ]
}
]
},
"links":[

]
}



As it can be seen above, OneToXXX fields are being taken to be serialized but no output for the ManyToOne fields like
Assigning nested entity serializer for @javax.persistence.ManyToOne ... com.my.OtherClass ... and therefore those aren't existed in the response JSON.


OneToXXX


ManyToOne


Assigning nested entity serializer for @javax.persistence.ManyToOne ... com.my.OtherClass ...



According to this SO answer, @ManyToXXX referenced entities are appended as links to the JSON response. But that's not an acceptable solution for me since I have a different planning of consumption in my mind for the rest client.


@ManyToXXX



Bottomline, I'd like to have my ManyToOne referenced entities in my JSON Response returned from getResults() method.


ManyToOne


getResults()



Anything I can provide just ask in the comments.



This question has not received enough attention.









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

VzqtDnwLp,H78,rYI7jvAOg,hsfhHmzfISc809Uojzu,heYM8AO6tQDEhBiqv5vkvjmZ
IAlwWhRhzP,F8G

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham