Mapping metadata information

132 views
Skip to first unread message

Veit Guna

unread,
May 28, 2017, 5:00:50 AM5/28/17
to mapstruct-users
Hi.

I'm thinking about switching from Dozer to your mapper.
One important aspect that needs to be fulfilled for this is, having information about the actual mappings during runtime.

Currently, I have my DTOs and my Entities where the mapping is done for.
I've created a generic way to formulate REST queries (using the DTOs) that are then transformed into JPA queries (Entities) based on the mappings.
So e.g. I need to know, that dto.name came from entity.name. Or that dto.address.street came from entity.customer.address.street - including type information.

My question is: is such mapping information currently available via the mapper API?

Filip Hrisafov

unread,
May 30, 2017, 12:43:07 PM5/30/17
to mapstruct-users
Hi Veit,

I suppose you need this information on runtime? I think that your question is really similar to #702 (if not the same). 

Depending when you need to have this information there could be a way to get. One way is to plugin in your own ModelElementProcessor SPI so you can have access to the Types that MapStruct is creating. This is what is written in the comments of the issue I linked. Keep in mind that this SPI is internal currently.

I also don't quite understand how exactly you need to use this mapping information. Maybe if you give us some example I can help you more in relation to what MapStruct can do

Veit Guna

unread,
May 31, 2017, 1:07:37 PM5/31/17
to Filip Hrisafov, mapstruct-users

Hi Filip.

Yes, the issue targets the same idea/requirement. So that means that currently there is no existing metainformation available at runtime right?

So switching over isn't an option for me at the moment. But thanks for the pointers!

Cheers
Veit

Filip Hrisafov

unread,
Jun 1, 2017, 12:23:29 PM6/1/17
to mapstruct-users, filip.h...@gmail.com
Hey Veit,

Yes currently this meta information is not available. If you are interested you can make a contribution for something like that. We are gladly going to help you out.

Cheers,
Filip

Tobse Fritz

unread,
Jul 6, 2020, 3:48:31 AM7/6/20
to mapstruct-users

We also had the problem to access the Mapping Annotations at runtime.
We use MapStruct between a business-model and our ui-model.
When a UI client wants to get sorted data, it can specify a field from the ui-model.
Our MapStructParser can get the corresponding business-model field-name and create the needed Criteria to sort it.

We solved this problem by using ASM (bytecode manipulation and analysis framework)
to read the Mapping-Annotation and provide it in a meta model. At the end it's possible to write something like:

Example (Also available on Github)

carbon (1).png

The mappingInfo.mapToTargetField("plz") returns the mapped field of if the BusinessModel (zip).

The full runnable and tested example is available in here:
https://github.com/TobseF/mapstruct-metadata-example
StackOverflow:
https://stackoverflow.com/questions/62675889/  

> Theoretically it's also possible to hook into the MapStruct annotation processing process and generate MetaModel Java classes (ModelElementProcessor). But I wasn't able to get it working. Adding additional Annotation processors is't straight forward and debugging Annotation processing during class compilation is more than cumbersome. Four our purpose the ASM way and a simple mapping is sufficient.  
Reply all
Reply to author
Forward
0 new messages