VIRGIN FOREST OF JAVA
不要埋头苦干,要学习,学习,再学习。。。。。
powered by R.Zeus

  Projection, aggregation, subselects, detatched criterias - its all there in the Hibernate 3 Criteria API. Let me show you some examples, starting with the new projection API. Suppose we have a User class and only want to query for its username:

session.createCriteria(User.class)
    . setProjection(Projection.property(\"username\"))
    .list();

would do exactly what we want. Of course we can also use projection for multiple properties, and combine it with all the other existing features of the Criteria API. For example

session.createCriteria(User.class).setProjection(
    Projections.propertyList()
        .add(Projection.property(\"firstname\"))
        .add(Projection.property(\"lastname\"))
    )
    .add(Restriction.gt(\"age\", 25))
    .list();

would give us the firstnames and lastnames of all users with an age larger than 25. We can also use aggregation - if we want to count the number of users with lastname “Black”, we can use

session.createCriteria(User.class)
    .setProjection(Projections.rowCount())
    .add(Restriction.eq(\"lastname\", \"Black\"))
    .list();

we can even use grouping and projection on associated objects:

session.createCriteria(User.class)
    .createAlias(\"roles\", \"r\")
    .setProjection( Projections.projectionList()
        .add(Projection.groupProperty(\"r.name\"))
        .add(Projection.rowCount())
    )
    .list();

This criteria query gives us the number of users, grouped by role names. With the new Criteria API extensions, you can also construct “detatched criteria” instances, which you can build outside of the session scope, and later use for querying. For example

DetatchedCriteria dc = DetatchedCriteria.forClass(User.class)
    .add(Property.forName(\"firstname\").eq(\"John\")
    .setProjection(Property.forName(\"age\").min());
	
Session session = sessionFactory.openSession();
	
dc.getExecutableCriteria(session).list();

would give use the age of the youngest user named John. You can even use the detatched criteria for building subqueries. For example

DetatchedCriteria dc = DetatchedCriteria.forClass(User.class)
    .add(Property.forName(\"firstname\").eq(\"John\")
    .setProjection(Property.forName(\"age\").min());
	
session.createCriteria(User.class)
    .add(Subqueries.propertyEq(\"age\", dc))
    .list();

would give us all Users which have the same age as the youngest User named John.

Alltogether the new H3 criteria API now is featurewise just as powerful as the HQL query language. If you want to try and experiment with it, download the newest Hibernate 3 beta from the Hibernate website

posted on 2006-09-29 14:30 R.Zeus 阅读(966) 评论(0)  编辑  收藏 所属分类: Hibernate

只有注册用户登录后才能发表评论。


网站导航: