2009-09-15 155 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 

不知道为什么休眠不应该是能够做到这一点 - 也许建议给他们)