Using "Chopped" annotation on DTOs
On the last post, I introduced Lazy-Chopper – A new library, that can chop your lazy associations of a graph, returned by the service layer to the client. There are some questions though: What are the constraints for the returned graph? Can we pass any object within it? What about collections - are they supported by the lazy chopper as well?
<!–[if gte mso 9]>
</xml><![endif]–><!–[if gte mso 9]>
</xml><![endif]–> <!–[if gte mso 10]>
Currently NOT any object is supported. We support any entity that inherits from yourabstract base class (possibly indirectly) AbstractEntity, any embedded object, and also collections of these them. As explained before, this means you do NOT have to write yet another parallel hierarchy for your domain model, and you can use your safely fetch a graph from the DB and send it to the client.
Nevertheless, what if you still want to send some kind of view as your return graph, encapsulated in a dedicated DTO class you have created? In other words, how can you still send a graph of objects containing DTO’s ? Please note that these DTOs, can still have associations to entities, which contain lazy associations, and thus by default, will bump in “LazyAssocationException” of Hibernate. What can we do with this common use case?
Luckily the LazyCopper supports this use case – All you need to do is mark your DTO class with the annotation Chopped. This annotation marks the lazyChopper to drill down on this returned DTO instance and search for lazy associations on its graph.
In summary, you can use LazyChopper for both entity graph, and in addition you can use it with any DTO marked with Chopped annotation.