-2

I am developing a spring restful application that uses hibernate. I am coming across scenarios where i had to place jackson json annotations on entity getters. Ex: One to Many mappings.

Is it a good idea to place jackson json annotations on hibernate entities? Or should i go with DTO pattern to pass data that is just needed by UI? I may end up creating a DTO for every entity.

Below is the application architecture. Common is at root level. DAO has dependency on Common and so on. Common <- DAO <- Services <- Web

DAO has entities Services or Web can have DTO's

Please let me know your thoughts or suggestions.

user3930151
  • 119
  • 1
  • 11

1 Answers1

0

Disclaimer: I am speaking from opinion and experience here

Separate dto and entities are common practice. What you choose to do really depends on it's use. One of the drawbacks of adding DTO annotations on your data model entities is versioning. Versioning becomes hard when your data model is tied to your contract. If your webservice is only used by a consumer you own and its deploy schedule is the same then it's probably not worth separating dto/entities. If you don't need versioning and you mostly have CRUD web services then you may want to look into spring data rest.

If you arn't so lucky, and have multiple consumers, then you may want to think through a few version breaking changes and how you will handle it. This will help you see the value in separating the data and contract.

Daniel Moses
  • 5,872
  • 26
  • 39