如何让Hibernate忽略子类? [英] How do I get Hibernate to ignore subclasses?

查看:69
本文介绍了如何让Hibernate忽略子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班级C1和C2.每个持久化到其自己的表.每个都有自己的Hibernate XML描述,并提及自己的表.这些类之间根本没有任何联系.

I have two classes C1 and C2. Each persists to its own table. Each has its own Hibernate XML description, mentioning its own table. There is no connection between these classes at all.

除了C1是C2的子类.

Except that C1 is a subclass of C2.

很明显,C1的XML从C2的XML复制了很多东西(添加了一些东西),但这是一个单独的副本.再说一次:XML文件都没有提到另一个类或另一个表.

Obviously C1's XML copies a lot from C2's (with some stuff added), but it's a separate copy. Again: neither XML file mentions the other class or the other table.

但是现在,当我执行"From C2"查询时,Hibernate会提取所有两个表.

But now, when I do a "From C2" query, Hibernate pulls in all of both tables.

从逻辑上讲,这是有道理的:C1实际上是C2,因此,如果我想要所有C2,我也需要C1,因此我们需要读取C1的所有表.Hibernate必须使用反射来检测C2的子类C.

Logically, this makes sense: a C1 is in fact a C2, so if I want all C2s I need the C1s as well, so we need to read all of C1's table. Hibernate must be using reflection to detect subclass C1 of C2.

假设这是预期的行为(尽管这不是我所期望的!)如何避免这种情况?我有什么办法告诉Hibernate,当我说"From C2"时,我的意思是仅C2而不是C1引导?我可以在查询中添加一个子句,将其限制为非C1类型的对象,从而避免查询C1表的性能下降吗?

Supposing this is expected behavior (though it wasn't expected by me!) how do I avoid it? Is there any way I can tell Hibernate that when I say "From C2" I really mean just C2 and not C1 to boot? Is there a clause I can add to the query that will restrict it to objects not of type C1, also avoiding the performance hit of querying C1's table in the first place?

推荐答案

从C2中选择时,它在休眠状态下默认也会选择C1.这称为多态查询.参见多态查询,和教程.

When you select from C2 it will also select C1 by default in hibernate. It's called Polymorphic queries. see Polymorphic queries , and Tutorial.

要修复/解决它,您应该添加类型为PolymorphismType.EXPLICIT的Polymorphism批注

To fix/solve it you should add Polymorphism annotation with type PolymorphismType.EXPLICIT

@Polymorphism(type = PolymorphismType.EXPLICIT)

@Polymorphism(type = PolymorphismType.EXPLICIT)

来自> https://docs.jboss.org/hibernate/orm/5.2/javadocs/

IMPLICIT-如果检索到其任何超级实体,则检索此实体.默认值,

IMPLICIT -This entity is retrieved if any of its super entity are retrieved. The default,

EXPLICIT-仅在明确要求时才检索此实体.

EXPLICIT -This entity is retrieved only if explicitly asked.

这篇关于如何让Hibernate忽略子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆