Possible solutions:
- Have you tried to add the attribute type in the parent class? This should be enough if you retrieve this field from the DB.
-
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"); }