We have a back-end component that exposes database (PostgreSQL) data via JPA to RESTful API.
The problem is that when sending a JPA entity as a REST response, I can
As a possible resolution, Vlad Mihalcea suggests that I don't bother with jackson-datatype-hibernate project
and simply construct a DTO. I've been trying to force Jackson to do what I want for 3 days 10-15 hours each, but I've given up.
I've looked at fetching principle from another side after reading Vlad's blog post on how EAGER fetching is bad in general -- I now understand, that it's a bad idea to try and define what property to fetch and what not to fetch only once for the whole application (that is inside the entity using fetch
annotation attribute of @Basic
or of @OneToMany
or @ManyToMany
). That will cause a penalty for additional lazy fetching in some cases or unnecessary eager fetching in other ones. That said, we need to create a custom query and a DTO for each GET endpoint. And for DTO we won't have any of those JPA related issues, which will also let us remove the datatype dependency.
There is more to discuss: as you can see in the code example, we couple JPA and HATEOAS together for convenience. While it's not that bad in general, considering the previous paragraph about "the ultimate property fetching choice" and that we create a DTO for each GET, we may move HATEOAS to that DTO. Also, releasing a JPA entity from extending a ResourseSupport
class lets it extend a parent that is actually relevant to the business logic.