問題
選擇MappedSuperclass我有一個@MappedSuperclass
稱爲數據爲每一個實體在我的數據庫中的父母。它包含像Id等通用屬性。然後,我有一個實體,擴展數據,這也是由於其子類的通用功能,也是一個@MappedSuperclass
。我的數據庫中的映射是正確的。從數據庫(休眠)
這裏是我的層次
@MappedSuperclass Data | @MappedSuperclass +- Employee | | @Entity | +- FullTimeEmployee | | @Entity | +- PartTimeEmployee | @Entity +- Store
的例子,該表是正確映射:
FullTimeEmployee PartTimeEmployee Store
反正有沒有在數據庫中查詢所有員工子類(FullTimeEmployee,PartTimeEmployee)的情況下,僱員沒有引用查詢中的子類名稱?
喜歡的東西
List<Employee> allEmployees = getAllEmployees();
的想法是,每當我決定創建一個員工(即AllDayEmployee)的另一個子我不會有更改查詢到包括名稱。
解決方案
所以,Gregory正確地指出,這是不可能的@MappedSuperclass
。所以我將它改爲@Entity,因爲我想爲每個子類保留一張表,我使用了InheritanceType.JOINED
。
所以上面的層次,現在是
@MappedSuperclass Data | @Entity | @Inheritance(strategy=InheritanceType.JOINED) +- Employee | | @Entity | +- FullTimeEmployee | | @Entity | +- PartTimeEmployee | @Entity +- Store
以及表依然:
FullTimeEmployee PartTimeEmployee Store
所以,現在,讓所有的員工我只需撥打:
entityManager.createQuery("from Employee").getResultList();
如果我需要FullTimeEmployee和PartTimeEmployee(我使用的是Oracle兩種不同的'@ SequenceGenerator'序列)?作爲一個實體我必須在Employee類中指定'@ Id'。 – drakyoko 2016-10-06 15:30:44