How to use ModelMapper for deep inheritance objects?

Possible solutions:

  1. Have you tried to add the attribute type in the parent class? This should be enough if you retrieve this field from the DB.
  2. A simple tested trick, add protected String type = this.getClass().getSimpleName().toLowerCase();. Notice that it must be protected. Here’s my tested code:

    @Data
    abstract class A {
      protected String type = this.getClass().getSimpleName().toLowerCase();
    }
    
     class AB extends A {}
    
     class AA extends A {}
    
     @Test
     public void test(){
        A a1 = new AB();
        A a2 = new AA();
        assertThat(a1.getType()).isEqualTo("ab");
        assertThat(a2.getType()).isEqualTo("aa");
     }
    

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)