Home hibernate DTO with a deviant database
Reply: 1

hibernate DTO with a deviant database

scramados
1#
scramados Published in 2017-12-07 22:00:52Z

I am building a spring boot application which uses REST services to deliver content to the front-end

But my DTO does not correspond to my database. Here is a diagram of the database And my DTO should look something like this

public class GlobeEntity extends BaseEntity {
//  for all definition years
    private List<Instance> instances;
    class Instance {
//      CountryInstance.definitionYear
        private String definitionYear;
//      for all countries
        private List<Country> countries;
        class Country {
//          Country.countryId
            String id;
//          Country.externalIdentifier
            String externalIdentifier;
//          CountryInstanceCompatibility.total
            String com;
//          CountryInstanceUtility.total
            String uti;
        }
    }
}

Is this possible to do with hibernate annotations?

Snickers3192
2#
Snickers3192 Reply to 2017-12-07 23:43:21Z

For the DTO, what is turned into JSON when you return that class from a controller depends on your JSON serializer you have configured in Spring. Then you make use of the JSON annotations to have more control over what is returned. Depending on how you do things, you may just choose to convert between an entity and a DTO, and have a class for each. Although this is a bit annoying because it does add allot of overhead of having more classes and other classes depending on the two, it can be come a real real mess if you use the same class for both DTO and Entity representations when they are very different. For instance if you use a method in your controller, it's not clear wether this is an entity or a DTO. There are many cases when you have a controller you return a response which is not a resource in a database.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.255977 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO