1 package org.hibernate.criterion; 3 4 import java.util.ArrayList ; 5 6 7 8 import org.hibernate.Criteria; 9 import org.hibernate.EntityMode; 10 import org.hibernate.HibernateException; 11 12 13 import org.hibernate.engine.TypedValue; 14 15 import org.hibernate.type.AbstractComponentType; 16 import org.hibernate.type.Type; 17 import org.hibernate.util.StringHelper; 18 19 23 public class InExpression implements Criterion { 24 25 private final String propertyName; 26 private final Object [] values; 27 28 protected InExpression(String propertyName, Object [] values) { 29 this.propertyName = propertyName; 30 this.values = values; 31 } 32 33 public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) 34 throws HibernateException { 35 String [] columns = criteriaQuery.getColumnsUsingProjection(criteria, propertyName); 36 String singleValueParam = StringHelper.repeat( "?, ", columns.length-1 ) + "?"; 37 if ( columns.length>1 ) singleValueParam = '(' + singleValueParam + ')'; 38 String params = values.length>0 ? 39 StringHelper.repeat( singleValueParam + ", ", values.length-1 ) + singleValueParam : 40 ""; 41 String cols = StringHelper.join(", ", columns); 42 if ( columns.length>1 ) cols = '(' + cols + ')'; 43 return cols + " in (" + params + ')'; 44 } 45 46 public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) 47 throws HibernateException { 48 ArrayList list = new ArrayList (); 49 Type type = criteriaQuery.getTypeUsingProjection(criteria, propertyName); 50 if ( type.isComponentType() ) { 51 AbstractComponentType actype = (AbstractComponentType) type; 52 Type[] types = actype.getSubtypes(); 53 for ( int i=0; i<types.length; i++ ) { 54 for ( int j=0; j<values.length; j++ ) { 55 Object subval = values[j]==null ? 56 null : 57 actype.getPropertyValues( values[j], EntityMode.POJO )[i]; 58 list.add( new TypedValue( types[i], subval, EntityMode.POJO ) ); 59 } 60 } 61 } 62 else { 63 for ( int j=0; j<values.length; j++ ) { 64 list.add( new TypedValue( type, values[j], EntityMode.POJO ) ); 65 } 66 } 67 return (TypedValue[]) list.toArray( new TypedValue[ list.size() ] ); 68 } 69 70 public String toString() { 71 return propertyName + " in (" + StringHelper.toString(values) + ')'; 72 } 73 74 } 75 | Popular Tags |