Ignore null values

295 views
Skip to first unread message

Vbuch

unread,
Sep 17, 2018, 9:13:08 AM9/17/18
to mapstruct-users
Hello Team, 

Any idea how to ignore null values / not set properties on the source to target mapping?

In my case, I am converting a DTO to a JPA Entity. My JPA entity has an auto-increment field (cust_id) which I do not want to insert manually upon saving. 
When I try to map DTO to Entity using map struct, map struct sets the ID to 0 or null (Please note I do not have ID property set on DTO).

I tried using ignore on target as follows but it still sets it. Any help on this will be highly appreciated.

 @Mapping(target = "cust_id", ignore = true)

Customer customerDtoToEntity(CustomerDto customerDto);


Thanks!

Filip Hrisafov

unread,
Sep 17, 2018, 2:19:43 PM9/17/18
to mapstruct-users
Hey,

Can you please share how the getter and setter for your `Customer` look like?

What do you mean with MapStruct sets the ID to 0 or null? How does the generated code look like?

Cheers,
Filip

Edson Alves Pereira

unread,
Sep 17, 2018, 4:43:36 PM9/17/18
to filip.h...@gmail.com, mapstru...@googlegroups.com
For my understanding, a null ID means that this object is not persisted to database, then is completely right mapstruct live that way, if you get the entity from the persistence layer it will have an ID with a valid value. That is the way it works for me and I use all my IDs generated by database.

--
You received this message because you are subscribed to the Google Groups "mapstruct-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapstruct-use...@googlegroups.com.
To post to this group, send email to mapstru...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Edson Alves Pereira

unread,
Sep 17, 2018, 4:45:45 PM9/17/18
to filip.h...@gmail.com, mapstru...@googlegroups.com
Just curious about, your IDs must be object as most of all properties to JPA, if mapstruct is giving you a 0 ID, maybe there is a change you are using a primitive property.
Reply all
Reply to author
Forward
0 new messages