spring-projects / spring-projects/spring-data-rest
Inheritance of entities/repositories [DATAREST-344]
@odrotbohm is already working on this.
Since Dec 31, 2020.
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
Benjamin M opened DATAREST-344 and commented
I'd like to use inheritance for my entities. And the only way of getting it kinda working was to do the following:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="type")
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type")
public abstract class Message implements Identifiable<UUID> { ... }
@Entity
@DiscriminatorValue("TEXT")
@JsonTypeName("TEXT")
public class TextMessage extends Message { ... }
@Entity
@DiscriminatorValue("TODO")
@JsonTypeName("TODO")
public class TodoMessage extends Message { ... }
public interface MessageRepo extends JpaRepository<Message, UUID> { }
public interface TextMessageRepo extends JpaRepository<TextMessage, UUID> { }
public interface TodoMessageRepo extends JpaRepository<TodoMessage, UUID> { }
When I now call GET http://localhost:8080/webapp/messages I get the following output:
{
"_links": {
"self": { "href": "http://localhost:8080/webapp/messages{?page,size,sort}", "templated": true }
},
"_embedded": {
"textMessages": [
{ ... },
{ ... }
],
"todoMessages": [
{ ... }
]
},
"page": {
"size": 20, "totalElements": 3, "totalPages": 1, "number": 0
}
}
As you can see, there's textMessages and todoMessages within the _embedded object. So, I cannot really sort my messages.
Is there a way to get all my messages within a single array?
—
EDIT: I tried to get rid of my additional projections, but that won't work:
If I only have a @Projection for Message.class, it won't get applied when calling GET http://localhost:8080/webapp/messages?projection=summary,
BUT it does work, if it's an inherited projection: GET http://localhost:8080/webapp/messageInboxes/89cb89db-67c5-49b3-8f1f-00b63b74ca4a?projection=summary
with:
@Projection(name = "summary", types = MessageInbox.class)
public interface MessageInboxSummary {
MessageSummary getMessage();
}
@Projection(name = "summary", types = Message.class)
public interface MessageSummary {
String getSubject();
}
@Entity
public class MessageInbox extends Identifiable<UUID> {
@ManyToOne
@JoinColumn(name = "Message_id", nullable = false, updatable = false)
Message message;
public Message getMessage() { return message; }
}
—
EDIT 2:
I now somehow fixed it / encountered a new bug...
If I put
@RepositoryRestResource(collectionResourceRel="messages", path="messages")
on all 3 repositories it behaves like I want it to: Every kind of message is available under GET http://localhost:8080/webapp/messages and all get displayed within the same array!
But now there are 2 issues:
- The index page displays:
{
"_links": {
"messages": [
{
"href": "http://localhost:8080/webapp/messages{?page,size,sort,projection}",
"templated": true
},
{
"href": "http://localhost:8080/webapp/messages{?page,size,sort,projection}",
"templated": true
},
{
"href": "http://localhost:8080/webapp/messages{?page,size,sort,projection}",
"templated": true
}
]
}
}
- if I add
itemResourceRel="message"to the@RepositoryRestResourceannotation on all 3 repositories, the URLGET http://localhost:8080/webapp/messageswill display randomly either theTextMessagesOR theTodoMessages, but never both
Affects: 2.1.1 (Dijkstra SR1)
9 votes, 16 watchers
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.