DTO projection with properties from joined entities
See original GitHub issueIt would be nice to be able to project also properties from joined entity into DTO.
Imagine that I have two entities:
@Entity
public class Recipe {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String sourceUrl;
@ElementCollection
private List<String> steps;
private LocalDateTime createdOn;
// + getters and setters
}
@Entity
public class ParseError implements Serializable {
@Id
@ManyToOne(fetch = FetchType.LAZY)
private Recipe recipe;
@Id
private String problemArea;
private String message;
// + getters and setters
}
Then I would like to have DTO for ParseError
like this:
@Introspected
public class ParseErrorDto {
private Integer recipeId;
private String recipeName;
private String problemArea;
private String message;
// + getters and setters
}
Where the framework can translate recipeId
DTO property into entity path recipe.id
. If it is too complex to resolve it automatically then at least using @Value("#{recipe.id}")
annotation as it is in Spring Data.
Or it can be like this:
@Introspected
public class ParseErrorDto {
private RecipeDto recipe;
private ParseError.ProblemArea problemArea;
private String message;
// + getters and setters
}
Where Recipe recipe
property from entity is mapped into RecipeDto recipe
property in DTO. And DTO for Recipe
entity can look like this:
@Introspected
public class RecipeDto {
private Integer id;
private String name;
// + getters and setters
}
Does it make sense? Would it be possible?
Thank you.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Micronaut Data DTO projection with properties from joined ...
I use Micronaut Data with JPA and have two entities. The first one is Recipe :
Read more >Entities or DTOs - When should you use which projection?
Entity projections are great for all write operations. Hibernate and any other JPA implementation manages the state of your entities and creates the...
Read more >How to fetch a one-to-many DTO projection with JPA and ...
While entities make it very easy to fetch additional relationships, when it comes to DTO projections, you need to use a ResultTransformer to ......
Read more >Spring Data JPA Projections - Baeldung
Behind the scenes, Spring creates a proxy instance of the projection interface for each entity object, and all calls to the proxy are...
Read more >How to Enrich DTOs With Virtual Properties Via Spring ...
In this tutorial, we learn how to fetch data from DTOs instead of JPA entities using virtual properties and Spring projections.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It does make sense and would potentially be possible. Thanks for the feedback.
It’s hard 😄
Will take another stab soon