KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > criterion > SizeExpression


1 //$Id: SizeExpression.java,v 1.10 2005/02/18 03:47:27 oneovthafew Exp $
2
package org.hibernate.criterion;
3
4
5 import org.hibernate.Criteria;
6 import org.hibernate.EntityMode;
7 import org.hibernate.Hibernate;
8 import org.hibernate.HibernateException;
9 import org.hibernate.engine.TypedValue;
10 import org.hibernate.persister.collection.QueryableCollection;
11 import org.hibernate.persister.entity.Loadable;
12 import org.hibernate.sql.ConditionFragment;
13
14 /**
15  * @author Gavin King
16  */

17 public class SizeExpression implements Criterion {
18     
19     private final String JavaDoc propertyName;
20     private final int size;
21     
22     protected SizeExpression(String JavaDoc propertyName, int size) {
23         this.propertyName = propertyName;
24         this.size = size;
25     }
26
27     public String JavaDoc toString() {
28         return propertyName + ".size=" + size;
29     }
30
31     public String JavaDoc toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
32     throws HibernateException {
33         String JavaDoc role = criteriaQuery.getEntityName(criteria, propertyName) +
34             '.' +
35             criteriaQuery.getPropertyName(propertyName);
36         QueryableCollection cp = (QueryableCollection) criteriaQuery.getFactory().getCollectionPersister(role);
37         //String[] fk = StringHelper.qualify( "collection_", cp.getKeyColumnNames() );
38
String JavaDoc[] fk = cp.getKeyColumnNames();
39         String JavaDoc[] pk = ( (Loadable) cp.getOwnerEntityPersister() ).getIdentifierColumnNames(); //TODO: handle property-ref
40
return "? = (select count(*) from " +
41             cp.getTableName() +
42             //" collection_ where " +
43
" where " +
44             new ConditionFragment()
45                 .setTableAlias( criteriaQuery.getSQLAlias(criteria, propertyName) )
46                 .setCondition(pk, fk)
47                 .toFragmentString() +
48             ")";
49     }
50
51     public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery)
52     throws HibernateException {
53         return new TypedValue[] { new TypedValue( Hibernate.INTEGER, new Integer JavaDoc(size), EntityMode.POJO ) };
54     }
55
56 }
57
Popular Tags