jmapper-framework / jmapper-core

Elegance, high performance and robustness all in one java bean mapper
http://jmapper-framework.github.io/jmapper-core
Apache License 2.0
227 stars 41 forks source link

How to avoid nested exception #26

Closed magmel48 closed 8 years ago

magmel48 commented 8 years ago

Hello, first of all, thank you for great library! I have one question according to nested mapping feature.

In example, I have Application class: class Application { Category category; ... } And Category class: class Category { Long id; ... }

I want to map Application to ApplicationDto class: class ApplicationDto { Long categoryId; ... }

For categoryId field I specified annotation @JMap("${category.id}") and everything works great, but I got one problem. My database in some places is inconsistent and I can't fix it in near future. Because of that sometimes I can get null in category field and this annotation at the moment of mapping throws exception. But Long type can store null and my question is - is it possible to avoid exception and provide null for categoryId if category field is also null?

avurro commented 8 years ago

Hi Max,

the only way that you have to solve this problem is by using Conversion method:

@JMapConversion(from={"category"}, to={"categoryId"})
public Long conversion(Long categoryId, Category category){
     return category == null ? null: category.getId();
}

and change @JMap("${category.id}") to @JMap("${category}")
This method is added to the configuration. so when you have solved the problems with the database you just delete it and restore the mapping.
there isn't a quicker solution at the moment