简体   繁体   中英

Override JPA MappedSuperClass generic property

I have the below scenario, I need sub-classes to be able to specify the actual types of properties found in MappedSuperClass(s). I use hibernate as a provider and I don't mind using hibernate specific annotations to solve this problem.

@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(){
      .....
   }
}

@AssociationOverride will hep you in this case. See the documentation for it (of course it is a JPA annotation). You could use it in combination with the @AttributeOverrides annotation for overriding basic types. Example (taken from the example):

@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 {
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM