我希望按实体扩展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独立解决方案......