1

I want to extend POJO abstract class by entity. Something like that:

public abstract class AbstractClientEntity {
    protected Long id;
    protected String name;
    protected String email;
    protected List<AccountEntity> accountEntities = new ArrayList<>();

    public AbstractClientEntity() {
    }

    public AbstractClientEntity(Long id, String name, String email, List<AccountEntity> accountEntities) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.accountEntities = accountEntities;
    }
    // public getters, setters, equals, hashCode
}

@Entity
public class ClientEntity extends AbstractClientEntity {
    public ClientEntity() {
    }

    public ClientEntity(Long id, String name, String email, List<AccountEntity> accountEntities) {
        super(id, name, email, accountEntities);
    }

    @Override
    @Id
    @GeneratedValue        
    public Long getId() {
        return id;
    }

    @Override
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    public List<AccountEntity> getAccountEntities() {
        return accountEntities;
    }
}

With this approach I get in DB cliententity table with only id column. But when I overrided all setters and getters in ClientEntity – it works fine (all fields mapped successfully).

@Entity
public class ClientEntity extends AbstractClientEntity {
    public ClientEntity() {
    }

    public ClientEntity(Long id, String name, String email, List<AccountEntity> accountEntities) {
        super(id, name, email, accountEntities);
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }
    @Override
    public String getName() {
        return name;
    }
    @Override
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String getEmail() {
        return email;
    }
    @Override
    public void setEmail(String email) {
        this.email = email;
    }
    @Override
    public void setAccountEntities(List<AccountEntity> accountEntities) {
        this.accountEntities = accountEntities;
    }
    @Override
    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    @Override
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    public List<AccountEntity> getAccountEntities() {
        return accountEntities;
    }
}

So the main question: how to not override all setters and getters in ClientEntity and achieve successful mapping?

AbstractClientEntity should NOT be an @Entity or @MappedSuperclass or something else JPA-specific. So my question differs from #1, #2, #3, #4.

I hope it is possible.

JSR-000338 Java Persistence 2.1 Specification (2.11 Inheritance, p 54) says:

Entities can extend non-entity classes and non-entity classes can extend entity classes.

Also I want to achieve Hibernate independent solution if it possible...

Woland
  • 623
  • 2
  • 13
  • 31
  • 1
    To persist the fields of the superclass you EITHER make the superclass a mappedSuperclass (which you don't want), OR you persist as properties (which you want to avoid seemingly). The other alternative is that you provide an `orm.xml` and define the abstract superclass as mapped superclass using that, so then the class is not touched by annotations. –  Oct 31 '17 at 08:12
  • @DN1 _"or you persist as properties"_ – I like this approach. I tried to implement it through inheritance. But why public methods from abstract class are not enough for mapping? Why Hibernate doesn't see them? It's just a simple inheritance... – Woland Oct 31 '17 at 09:04
  • It works as defined in the specification. If JPA would consider everything that is in a superclass you would end up in defining all the properties transient in the case you don't want to have the properties on the database table. So in my opinion @MappedSuperclass is the better solution. – Simon Martinelli Oct 31 '17 at 09:54
  • @72Services , @DN1 What can you recommend in my situation: I need to implement 2 persistence layers (one for MySQL with `@Entity` classes and other one for MongoDB driver with no using of ORM frameworks)? I thought it was a good idea to create abstract class and two implementations (@Entity and POJO class which I will map myself). Now I see a way out of the situation only in interfaces. – Woland Oct 31 '17 at 10:38
  • Why do you think that @MappedSupperclass would be problem when you use Mongo? – Simon Martinelli Oct 31 '17 at 19:15

0 Answers0