Is it possible to use hibernate entity classes as POJOs for GSON?

Viewed 395

I'm working on a project where we have a spring boot application that uses spring data and hibernate. Now I want to use the GSON library to work with JSON files. I've read a tutorial where it becomes clear that it is possible to create POJO classes and convert JSON files into objects from those classes. The same thing happens with hibernate.

Now my question: is it possible to design the POJO (or entity) classes in a way that they work both for hibernate and GSON. Can problems arise, if it is possible and I do it it that way?

Thanks in advance!

Edit: Here is the tutorial where I read about POJOs for GSON: tutorials point - GSON

2 Answers

It is possible, but it is not a good design. If you use it for example serialize rest interface data, then it will hard couple your rest endpoint with database or it can lead for security issues (serialize sensitive data) etc. So it can lead hard coupling, and will be harder to decouple it latter. It is always good to create separate model to db and other interfaces. You can use mapping libraries (for example mapstuct) to mapping between the models easily.

Related