我有以下场景,我需要子类才能指定MappedSuperClass中找到的实际属性类型。我使用hibernate作为提供程序,我不介意使用hibernate特定的注释来解决这个问题。
@MappedSuperclass
abstract class BaseA{
....
@OneToMany(mappedBy = "baseA")
public Set<? extends BaseB> getBaseB(){
.....
}
}
@MappedSuperclass
abstract class BaseB{
.....
@ManyToOne(optional = false)
@JoinColumn(name = "basea_id")
public BaseA getBaseA(){
.....
}
}
@Entity
class BaseAImpl extends BaseA{
public Set<BaseBImpl> getBaseB(){
.....
}
}
@Entity
class BaseBImpl{
public BaseAImpl getBaseA(){
.....
}
}
答案 0 :(得分:2)
@AssociationOverride
会解雇你。请参阅documentation(当然它是JPA注释)。您可以将它与@AttributeOverrides
注释结合使用以覆盖基本类型。示例(摘自示例):
@MappedSuperclass
public class Employee {
...
@ManyToOne
protected Address address;
...
}
@Entity
@AssociationOverride(name="address",
joinColumns=@JoinColumn(name="ADDR_ID"))
// address field mapping overridden to ADDR_ID foreign key
public class PartTimeEmployee extends Employee {
...
}