spring-projects / spring-projects/spring-data-rest
Inconsistent handling of polymorphic entities with only a repository for the root entity type
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
If I define a hierarchy of entities, and a polymorphic repository only for the root entity, descendants of the root entity are included under the management of the polymorphic repository only if it exists at least another entity (with a repository) with a linkable association to the specific subclass.
For example, given the following:
@Entity
@Inheritance(strategy = SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = STRING)
public abstract class Address {
...
}
@Entity
@DiscriminatorValue("Residential")
public class ResidentialAddress extends Address {
}
@Entity
@DiscriminatorValue("Business")
public class BusinessAddress extends Address {
}
@Entity
public class Person {
...
@OneToOne(fetch = FetchType.LAZY)
private ResidentialAddress residentialAddress;
...
}
@RepositoryRestResource(path = "addresses", collectionResourceRel = "addresses")
public interface AddressRepository extends JpaRepository<Address, Long> {
}
@RepositoryRestResource(path = "people", collectionResourceRel = "people")
public interface PersonRepository extends JpaRepository<Person, Long> {
}
then, ResidentialAddress will be handled by the AddressRepository and its rels and href will reflect the ones specified on the AddressRepository.
The BusinessAddress, instead, while still returned by the AddressRepository, it will have rels and href generated using the Evo Inflector, which returns an invalid href for the links (businessAddress) which is not mapped and you will get a 500 if you tried to retrieve it under the link specified URL.
The following is an example of the payload returned by querying the collection resource '/addresses':
{
"_embedded" : {
"addresses" : [ {
"label" : "Home",
"address" : "The Elves",
"_links" : {
"self" : {
"href" : "http://localhost/addresses/1"
},
"address" : {
"href" : "http://localhost/addresses/1"
}
}
} ],
"businessAddresses" : [ {
"label" : "Holiday",
"address" : null,
"_links" : {
"self" : {
"href" : "http://localhost/businessAddress/2"
},
"businessAddress" : {
"href" : "http://localhost/businessAddress/2"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost/addresses"
},
"profile" : {
"href" : "http://localhost/profile/addresses"
}
},
"page" : {
"size" : 20,
"totalElements" : 2,
"totalPages" : 1,
"number" : 0
}
}
My expectation is that entities in a hierarchy should be handled by their specific repository or by the next specified ancestor's repository.
This is a demo project demonstrating the issue: demo.zip
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.