in my hibernate project i use getters and setters to save data. In every time i need to use get method and set method for each variable to save. the question is that is there any convenient way to convert DTO to entity class? reagrds
Asked
Active
Viewed 1,586 times
0
-
You could use a library to map java beans Take a look this answer you can find some intersting libraries for such thing: http://stackoverflow.com/a/1432956/4751165 – Pau Nov 14 '16 at 12:01
-
Possible duplicate of [any tool for java object to object mapping?](http://stackoverflow.com/questions/1432764/any-tool-for-java-object-to-object-mapping) – Pau Nov 14 '16 at 12:02
1 Answers
0
I created Blaze-Persistence Entity Views for exactly that use case. You essentially define DTOs for JPA entities as interfaces and apply them on a query. It supports mapping nested DTOs, collection etc., essentially everything you'd expect and on top of that, it will improve your query performance as it will generate queries fetching just the data that you actually require for the DTOs.
An entity view example could look like this
@EntityView(Person.class)
interface PersonDto {
@Mapping("CONCAT(firstName, ' ', lastName)")
String getName();
}
Querying could look like this
List<PersonDto> dtos = entityViewManager.applySetting(
EntityViewSetting.create(PersonDto.class),
criteriaBuilderFactory.create(em, Person.class)
).getResultList();

Christian Beikov
- 15,141
- 2
- 32
- 58