2013-03-21 51 views
1

假設我有:用JPA映射多態列表?

//inheritance annotations 
public abstract class Animal { 
    private int noOfLegs; 
    //... 
} 

//inheritance annotations 
public class Cat extends Animal { 
    //cat stuff 
} 

//inheritance annotations 
public class Dog extends Animal { 
    //dog stuff 
} 

@Entity 
public class House { 
    @OneToMany 
    @JoinTable(//join stuff 
    private List<Animal> animals; 
    //hose stuff 
} 

現在,如果我把我的房子都貓狗,並堅持我的房子,並JPA知道把狗和貓在其相應的表(或任何繼承映射策略是用過的)。它也知道如何在查詢時收回它?

回答

2

簡短的回答是肯定的。但關於繼承映射的本地hibernate支持比標準JPA更強大,並且「每個類層次的單個表」或「每個子表的表」映射策略比「每個具體類的表」策略更適合多態查詢和關聯。

參見:

+0

即使文檔鏈接可能是問題的有用的解決方案將被大概理解以及。 – 2017-08-16 19:48:54