1 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 17 public class SizeExpression implements Criterion { 18 19 private final String propertyName; 20 private final int size; 21 22 protected SizeExpression(String propertyName, int size) { 23 this.propertyName = propertyName; 24 this.size = size; 25 } 26 27 public String toString() { 28 return propertyName + ".size=" + size; 29 } 30 31 public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) 32 throws HibernateException { 33 String role = criteriaQuery.getEntityName(criteria, propertyName) + 34 '.' + 35 criteriaQuery.getPropertyName(propertyName); 36 QueryableCollection cp = (QueryableCollection) criteriaQuery.getFactory().getCollectionPersister(role); 37 String [] fk = cp.getKeyColumnNames(); 39 String [] pk = ( (Loadable) cp.getOwnerEntityPersister() ).getIdentifierColumnNames(); return "? = (select count(*) from " + 41 cp.getTableName() + 42 " 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 (size), EntityMode.POJO ) }; 54 } 55 56 } 57 | Popular Tags |