Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BO <=> DTO mapper in Java

I am currently in my application maping DTOs to BO (and vice versa) manually. However, this approach is awkward and clumsy.

Is there any good mapper between these two representations?

My requirements follow:

  • Should support conversion of JPA proxies to identifiers (DTO should not link to other DTO directly). Or this functionality should be easy to implement
  • Should be annotation based, covention over configuration
  • Soft criteria: should allow mapping of multiple DTOs to one entity (and vice versa)

Thanks for any suggestions.

like image 874
malejpavouk Avatar asked Jan 25 '13 14:01

malejpavouk


2 Answers

Regarding object mapping I would recommend

  • spring 3 Object Mapping
  • modelmapper
  • dozer
  • orika
  • jDTO

Also, refer to this SO answer. It has a more or less complete list of Java Object mappers: https://stackoverflow.com/a/1432956/1137735

The 3 I suggested seemed more appealing to me. I think they all fulfill the requirements you ask.

like image 117
miguelcobain Avatar answered Sep 30 '22 00:09

miguelcobain


Well I know this thread is a bit old, and I am pretty sure that @miguelcobain answer is great.

Personnaly, I would recommend using Orika for a runtime sytem. It is strong and uses byte code generation at runtime so mapping is handled by generated code instead of always using the Reflection API. The other listed libraries are always using complex configuration and not conventions.

The second solution and the better one, I think would be to use Selma. This short library does the job for you, but instead of handling the mapping at runtime, it generates the mapping code at compile time using an annotation processor. So compiler will raise mapping errors, this is refactoring proof and you will be able to see the generated code.

Hope you'll give it a try.

like image 25
slemesle Avatar answered Sep 29 '22 22:09

slemesle