我将从一个经过净化的示例开始。
在我的系统中,我有 Car 类。 Car 有很多字段,其中有 GearShift 类的 gearShift 实例。
public class Car {
private GearShift gearShift;
// Snip
}
GearShift 是一个抽象类,AutomaticShift 和StickShift 继承自该类。这在 Hibernate 中映射为每个子类一个表。
现在,假设我想购买配备自动变速装置的汽车。我更喜欢通过 Hibernate 标准来执行此操作,因此我设想了一个我可以添加的“ofType”限制,如下所示。
getSession().createCriteria(Car.class)
.add(Restrictions.ofType(AutomaticShift.class)
.list();
这有可能吗?
最佳答案
旧:
这个怎么样?
getSession().createCriteria(AutomaticShift.class).list()
编辑:
这应该可以解决问题;
getSession().createCriteria(Car.class).createAlias("gearShift", "gs").add(Restrictions.eq("gs.class", AutomaticShift.class)).list();
关于java - 使用特定子类查找行的条件查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10702998/