Java – is there a way to expose hibernate entities as restful resources without dto?
I'm developing a simple webapp that exposes the domain model as a restful resource
When grouping hibernate entities into XML / JSON, if the entity is separated, it will throw lazyloadingexception for lazy sub Association If the entity is still attached to hibernate session, it will almost load the entire database
I have tried to use dozer customfieldmapper to determine whether the property is an inert hibernate collection, which is not loaded and then returns null
However, if we have two-way Association, hibernate will eagerly load many to one, and dozer will try to copy attributes, which will eventually lead to infinite loop and stackoverflow error
The only thing I know to solve this problem is to use dto and copy the required attributes only into a clean POJO (dto), and then group them into XML / JSON But manually copying attributes in complex domain models is very painful
Is there any other clean / simpler way (UN) to group hibernate entities?
Solution
I encountered a similar problem when passing hibernate VO back and forth in GWT application, and dozer works well in some projects, while the method described in this article is used in other projects, which is basically an empty hibernate agent before grouping
I hope I can help you,