spring-projects / spring-projects/spring-data-rest

JPA @ManyToMany associated entity not returned in json results [DATAREST-795]

Open
#1,171 2 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

type: bug
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

coolnodje opened DATAREST-795 and commented

When using a SampleRepository for a Sample entity, the @ManyToMany mapping doesn't appear in the return json when querying instance of Sample.

The mapping in Sample entity:
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "SAMPLE_AUTHORITY_INDEX",
joinColumns = {@JoinColumn(name = "sample_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "authority_label", referencedColumnName = "label")})
private Set<Authority> authorities = new HashSet<>();

The Sample#1 has an authority called LABEL1 which won't show up when querying the rest-data api:

curl http://localhost:8080/samples/1
{
"name" : "john",
"authorities" : [ { } ],
"link" : {
"self" : {
"href" : "http://localhost:8080/samples/1"
},
"sample" : {
"href" : "http://localhost:8080/samples/1"
}
}
}
The equivalent :
curl http://localhost:8080/samples/search/findByName?name=john
{
"name" : "john",
"authorities" : [ { } ],
"link" : {
"self" : {
"href" : "http://localhost:8080/samples/1"
},
"sample" : {
"href" : "http://localhost:8080/samples/1"
}
}
}%

also return empty authorities.

Though when implementing my own endpoint calling the above mentioned 'findByName' on SampleRepository, I get the authority content:

curl http://localhost:8080/v1/find/john
{"id":1,"name":"john","authorities":[{"label":"LABEL1"}]}%

Looking at the controller that handles the request, RepositorySearchController.java on executeSearch(...) method on line 174 contains the authority collection when it return new ResponseEntity(...) on line 185. I don't understand what happens after that and why the collection content doesn't make it to the json output


Affects: 2.4.4 (Gosling SR4)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.