开发者

Hibernate Criteria runtime class

This is my hierarchy:

// Table a
class A {}

// Table(" b
class B extends A {}

// Table my_class
class MyClass {

    A a;
}

I want to retrieve all MyClass objects from database with a relation to B but not to A.

B is a joined-subclass (extension of the table a by id).

My idea was something :

Criteria criteria = session.createCriteria(MyClass.class);
criteria.add(Restrictions.eq("a.class", B.class);

But it outputs an error:

could 开发者_开发百科not resolve property: a.class of a.b.MyClass

This is the simplest way I could put it. Bear in mind that the query is a bit more complicated.

Regards.

Udo.


I usually write a DetachedCriteria which selects all B's and filter MyClass where A in AllBs:

DetachedCriteria allBs = DetachedCriteria
  .forClass(B.class)
  .setProjection( Projections.property("id") );

Criteria  criteria = session.createCriteria(MyClass.class)
  .add(Subqueries.In("a", allBs);

(There may be errors, I'm not a java programmer.)

Creates something like:

select ...
from MyClass
where A in (select id from A inner join B on ...)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜