spring-projects / spring-projects/spring-data-rest
Using different projection for each entity subtype [DATAREST-739]
@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
Anton Koscejev opened DATAREST-739 and commented
Currently it's not possible to use projections with inheritance to manage which fields from each sub-type are shown.
Consider example from DATAREST-344 but with excerpt projections:
@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 { ... }
@Projection(name = "summary", types = TodoMessage.class)
public class TodoMessageSummary { ... }
@Projection(name = "summary", types = TextMessage.class)
public class TextMessageSummary { ... }
public interface MessageRepo extends JpaRepository<Message, UUID> { ... }
@RepositoryRestResource(excerptProjection = TodoMessageSummary.class)
public interface TodoMessageRepo extends JpaRepository<Message, UUID> { ... }
@RepositoryRestResource(excerptProjection = TextMessageSummary.class)
public interface TextMessageRepo extends JpaRepository<TextMessage, UUID> { ... }
First issue: how to define an excerpt projection for MessageRepo to use TodoMessageSummary for TodoMessage entities and TextMessageSummary for TextMessage?
Second issue: how to define projection for another entity that has a Message field? Let's say you have the following:
@Projection(name = "summary", types = Dashboard.class)
public class DashboardSummary {
List<Message> getMessages();
}
Using List<Message> works as expected: you get a list of messages, where each message could be TodoMessage or TextMessage and will have fields appropriate for its type. However, I want manage which fields are show for each sub-type of Message. (Using nexted projections for collections was introduced in DATAREST-394.) I cannot do this by specifying TodoMessageSummary or TextMessageSummary. What would I need to specify to make Spring Data REST choose TodoMessageSummary if it's a TodoMessage and TextMessageSummary if it's a TextMessage?
Affects: 2.4.1 (Gosling SR1), 3.1.9 (Lovelace SR9)
Issue Links:
- DATAREST-747 ProjectionDefinitionConfiguration does not select projection for most concrete type
("depends on")
10 votes, 9 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.