Hibernate 继承查询(条件)
我有以下两个类(Java Hibernate):
public class Grupo{
//plain Attributes
}
并且
public class Salon extends Grupo{
//plain Attributes
}
拥有两种类型的对象。
使用以下条件查询:
s.createCriteria(Grupo.class).list();
我得到了所有 Grupo 类型的对象,即 Grupo 和 Salon,如预期的那样。所以,我想知道的是,标准查询中是否有一种方法可以轻松地仅获取不是“Salon”对象的“Grupo”对象。如果可能的话,我想避免使用鉴别器(在数据库中,两个类都有自己的表)
任何帮助将不胜感激。
编辑:纠正了错误的java语法,我真是蹩脚。
I have the following two classes (Java Hibernate):
public class Grupo{
//plain Attributes
}
and
public class Salon extends Grupo{
//plain Attributes
}
having objects of both types.
using the following Criteria Query:
s.createCriteria(Grupo.class).list();
I get all the Grupo-type objects, that is Grupo and Salon, as expected. So, what I want to know is that if there is a way in Criteria Query to easily get only the "Grupo" objects that are not "Salon" objects. I will like to refrain of using discriminators if possible (in db both classes have their own tables)
Any help will be highly appreciated.
EDIT: Corrected wrong java syntax, how lame of me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
概念问题是所有 Salon 对象都是 Grupos。
您能否更改 Salon,使其不再派生自 Grupo,而是 Grupo 和 Salon 共享一个基类?
否则,你将不得不使用鉴别器。
The conceptual problem is that all Salon objects are Grupos.
Could you change Salon so that it doesn't derive from Grupo and instead Grupo and Salon share a base class?
Otherwise, you'll have to use discriminators.
如果您不必使用 Criteria,那么您可以使用 hql 来完成此操作
据我所知,使用 Criteria 您必须有一个鉴别器列才能正常工作。
如果你的 Grupos 判别器值为 G。
@Mark Robinson 建议的共享基类方法也值得考虑......
If you don't have to use Criteria then you can do this with hql
As far as I know, with Criteria you must have a discriminator column for this to work.
if your discriminator value for Grupos is G.
The shared base class approach suggested by @Mark Robinson is also worth considering...