JPA实体扩展了POJO抽象类(映射问题)

时间:2017-10-31 01:34:58

标签: java hibernate jpa jpa-2.0

我希望按实体扩展POJO抽象类。这样的事情:

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;
    }
}

通过这种方法,我只进入cliententity列的id表。但是当我在ClientEntity中覆盖了所有的setter和getter时 - 它工作正常(所有字段映射成功)。

@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;
    }
}

所以主要问题是:如何不覆盖ClientEntity中的所有setter和getter并实现成功映射?

AbstractClientEntity不应该是@Entity@MappedSuperclass或其他JPA特定的内容。所以我的问题与#1#2#3#4不同。

我希望有可能。

JSR-000338 Java Persistence 2.1 Specification (2.11继承,第54页)说:

  

实体可以扩展非实体类,非实体类可以扩展   扩展实体类。

如果可能的话,我想实现Hibernate独立解决方案......

0 个答案:

没有答案