question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to exclude a property of a nested multi-occurance element.

See original GitHub issue

I have two classes: A and B. A has a property items which is a collection of B, when mapping I would like to exclude the id property of A and of all B items. How can I achieve it? The closest thing I found in the user guide is the names{fullName} example when working with nested multi-occurrence elements (nothing on exclusion though) - so I tried to do something similar to exclude the id of B but it results in the exclusion of the entire items property (needless to say that A’s id gets excluded properly).

@Entity
public class A {

    @Id
    @GeneratedValue
    private Long id;
    //...

    @OneToMany()
    @JoinColumn(name = "a_id")
    private Set<B> items;

    // getters and setters
}

@Entity
public class B {

    @Id
    @GeneratedValue
    private Long id;
    //...

    public B() {}

    // getters and setters
}

MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        mapperFactory.classMap(A.class, A.class)
                .mapNulls(true)
                .exclude("id")
                .exclude("items{id}")
                .byDefault()
                .register();

MapperFacade mapperFacade = mapperFactory.getMapperFacade();
A dest = mapperFacade.map(source, A.class);

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
medvedniccommented, Jul 10, 2017

I’ve successfully achieved my goal using your suggestion to create a custom class map builder, it is much more convenient than my attempted workaround. I will post Gist later this week.

1reaction
elaatificommented, Jul 7, 2017

So basically you want to exclude id everywhere ? to do that you need to create a custom class map builder with a custom byDefault method.

https://gist.github.com/elaatifi/5212119

here is an example of how to create custom class map builders, for instance this one is supporting annotations, but you can provide your own rules : exclude id fields or any other generic rule.

Read more comments on GitHub >

github_iconTop Results From Across the Web

exclude nested children - jquery - Stack Overflow
The idea is to remove any inputs, selects, or textareas, which have a closest parent other than your desired parent. Notice also that...
Read more >
Exclude - Rocket Software Documentation
In a static server page, exclude the field or entity if it does not need to be sent to the ... Entity and...
Read more >
Select layers and objects – Figma Help Center
Select all layers. The Edit menu allows to select multiple objects based on their properties. This allows you to select all layers in...
Read more >
JSON methods, toJSON - The Modern JavaScript Tutorial
The space argument is used exclusively for a nice output. Here space = 2 tells JavaScript to show nested objects on multiple lines,...
Read more >
9 Subsetting R Objects | R Programming for Data Science
The [ operator can be used to extract multiple elements of a vector by ... can take an integer sequence if you want...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found