开发者

hibernate createCriteria

开发者 https://www.devze.com 2023-02-04 08:54 出处:网络
From Hibernate source, in Criteria.class, we have: List cats = session.createCriteria(C开发者_如何学Goat.class)

From Hibernate source, in Criteria.class, we have:

List cats = session.createCriteria(C开发者_如何学Goat.class)
  .createCriteria("kittens")
      .add( Restrictions.like("name", "Iz%") )
  .list();

What is 'kittens' here? The name of a column? Are not column names specified by using a ProjectionList?

Thanks


Here is the complete example from the Hibernate docs:

16.4. Associations

By navigating associations using createCriteria() you can specify constraints upon related entities:

List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.like("name", "F%") )
    .createCriteria("kittens")
      .add( Restrictions.like("name", "F%") )
    .list();

The second createCriteria() returns a new instance of Criteria that refers to the elements of the kittens collection.

So, "kittens" is a collection-type property for your Cat entity, for which a nested criterion is created, which constrains the kittens to only have names starting with F.

I think this means that you will only get Cats named starting with F with at least one kitten named starting with F.

List cats = session.createCriteria(Cat.class)
 .createCriteria("kittens")
   .add( Restrictions.like("name", "Iz%") )
 .list();

In your example, it returns all Cats with at least one kitten named starting with Iz.

I find this syntax kind of confusing, since the nesting is flattened. A Java source code formatter will also take away the helpful indentation.

Are not column names specified by using a ProjectionList?

This is not a projection. You are still getting all "columns". This is selection (a WHERE clause in SQL terms).


Kittens is a collection belonging to Cat. This query will return a List of Cat objects whose kittens collections contain only Cats starting with "Iz".

Edit: see Thilo's answer.

0

精彩评论

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

关注公众号