2009-09-15 140 views
9

問題

選擇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(); 
+0

如果我需要FullTimeEmployee和PartTimeEmployee(我使用的是Oracle兩種不同的'@ SequenceGenerator'序列)?作爲一個實體我必須在Employee類中指定'@ Id'。 – drakyoko 2016-10-06 15:30:44

回答

8

沒有,如果你正在使用@MappedSuperclass

這樣做的原因是,當你定義基類@MappedSuperclass,有基類不產生表,而不是所有屬性的混凝土被複製表。在您的示例中,只有FullTimeEmployee,PartTimeEmployee和Store表存在。

如果您希望能夠查詢基類實體,您需要爲基類選擇不同的映射。在基類上使用@Inheritance註釋,並選擇3種可能的映射策略之一 - 單表,每類表或連接

0

FROM Employee WHERE Employee.<employee only properties> = someValue 

但是,正如其他人在這裏所說的那樣,如果Employee實體被映射。你甚至不需要將它映射到它自己的表格。請參閱Hibernate中的映射策略。

+0

+1是的。在Hibernate中,你可以選擇一個抽象類,你也會得到所有的子類。給出的例子是查詢Object,它將返回整個數據庫! :-) – KLE 2009-09-15 14:20:49

+0

鑑於Employee是一個具有@MappedSuperclass註解的抽象類,我想獲得Employee的所有子類實例。 「從員工」查詢拋出一個QuerySyntaxException:員工沒有映射「 – pek 2009-09-15 14:24:21

+0

請看我的答案,爲什麼它不會與@MappedSuperclass – 2009-09-15 14:27:00

0

我似乎可以在hibernate 5.0.8中做到這一點(儘管使用InheritanceType.JOINED) ,Java 1.8.0_73和Oracle 12c - 要麼我誤解了,要麼冬眠已經改變了..

我有以下hierarhcy:

@MappedSuperclass 
@Inheritance(strategy=InheritanceType.JOINED) 
CommonRoot 
| 
| @MappedSuperclass 
+- Mapped 
     | @Entity(name="Concrete1") 
     | @Table(name="CON1") 
     +- Concrete1 
     | 
     | @Entity(name="Concrete2") 
     | @Table(name="CON2") 
     +- Concrete2 

而且我可以做以下HQL:

SELECT entityId FROM com.hibernatetest.Mapped ORDER BY entityId ASC 

這給這2個SQL語句:

select concrete2x0_.entityId as col_0_0_ from CON2 concrete2x0_ order by concrete2x0_.entityId ASC 
select concrete1x0_.entityId as col_0_0_ from CON1 concrete1x0_ order by concrete1x0_.entityId ASC 

和警告

WARN: HHH000180: FirstResult/maxResults specified on polymorphic query; applying in memory! 

不知道他們的意思,雖然,因爲這可以用SQL來完成的:

(select entityId from CON2 
union all 
select entityId from CON1) 
order by entityId ASC 

(你也可以加限制/ ROWNUM子句,如果你的願望,雖然這樣做有點笨重:

select * from (
(select * from (select entityId from CON2 order by entityId ASC) where rownum <= 10) 
UNION ALL 
(select * from (select entityId from CON1 order by entityId ASC) where rownum <= 10) 
) where rownum <= 10 order by entityId ASC 

不知道爲什麼休眠不應該是能夠做到這一點 - 也許建議給他們)