question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DTO projection with properties from joined entities

See original GitHub issue

It 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:open
  • Created 4 years ago
  • Reactions:3
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
graemerochercommented, Sep 30, 2019

It does make sense and would potentially be possible. Thanks for the feedback.

7reactions
graemerochercommented, Nov 12, 2020

It’s hard 😄

Will take another stab soon

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found