1 package org.hibernate.persister.collection; 2 3 import org.hibernate.Hibernate; 4 import org.hibernate.QueryException; 5 import org.hibernate.persister.entity.PropertyMapping; 6 import org.hibernate.type.Type; 7 8 11 public class CollectionPropertyMapping implements PropertyMapping { 12 13 private final QueryableCollection memberPersister; 14 15 public CollectionPropertyMapping(QueryableCollection memberPersister) { 16 this.memberPersister = memberPersister; 17 } 18 19 public Type toType(String propertyName) throws QueryException { 20 if ( propertyName.equals(CollectionPropertyNames.COLLECTION_ELEMENTS) ) { 21 return memberPersister.getElementType(); 22 } 23 else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_INDICES) ) { 24 if ( !memberPersister.hasIndex() ) throw new QueryException("unindexed collection before indices()"); 25 return memberPersister.getIndexType(); 26 } 27 else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_SIZE) ) { 28 return Hibernate.INTEGER; 29 } 30 else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_MAX_INDEX) ) { 31 return memberPersister.getIndexType(); 32 } 33 else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_MIN_INDEX) ) { 34 return memberPersister.getIndexType(); 35 } 36 else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_MAX_ELEMENT) ) { 37 return memberPersister.getElementType(); 38 } 39 else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_MIN_ELEMENT) ) { 40 return memberPersister.getElementType(); 41 } 42 else { 43 throw new QueryException("illegal syntax near collection: " + propertyName); 45 } 46 } 47 48 public String [] toColumns(String alias, String propertyName) throws QueryException { 49 if ( propertyName.equals(CollectionPropertyNames.COLLECTION_ELEMENTS) ) { 50 return memberPersister.getElementColumnNames(alias); 51 } 52 else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_INDICES) ) { 53 if ( !memberPersister.hasIndex() ) throw new QueryException("unindexed collection in indices()"); 54 return memberPersister.getIndexColumnNames(alias); 55 } 56 else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_SIZE) ) { 57 String [] cols = memberPersister.getKeyColumnNames(); 58 return new String [] { "count(" + alias + '.' + cols[0] + ')' }; 59 } 60 else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_MAX_INDEX) ) { 61 if ( !memberPersister.hasIndex() ) throw new QueryException("unindexed collection in maxIndex()"); 62 String [] cols = memberPersister.getIndexColumnNames(alias); 63 if ( cols.length!=1 ) throw new QueryException("composite collection index in maxIndex()"); 64 return new String [] { "max(" + cols[0] + ')' }; 65 } 66 else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_MIN_INDEX) ) { 67 if ( !memberPersister.hasIndex() ) throw new QueryException("unindexed collection in minIndex()"); 68 String [] cols = memberPersister.getIndexColumnNames(alias); 69 if ( cols.length!=1 ) throw new QueryException("composite collection index in minIndex()"); 70 return new String [] { "min(" + cols[0] + ')' }; 71 } 72 else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_MAX_ELEMENT) ) { 73 String [] cols = memberPersister.getElementColumnNames(alias); 74 if ( cols.length!=1 ) throw new QueryException("composite collection element in maxElement()"); 75 return new String [] { "max(" + cols[0] + ')' }; 76 } 77 else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_MIN_ELEMENT) ) { 78 String [] cols = memberPersister.getElementColumnNames(alias); 79 if ( cols.length!=1 ) throw new QueryException("composite collection element in minElement()"); 80 return new String [] { "min(" + cols[0] + ')' }; 81 } 82 else { 83 throw new QueryException("illegal syntax near collection: " + propertyName); 85 } 86 } 87 88 91 public String [] toColumns(String propertyName) throws QueryException, UnsupportedOperationException { 92 throw new UnsupportedOperationException ( "References to collections must be define a SQL alias" ); 93 } 94 95 public Type getType() { 96 return memberPersister.getCollectionType(); 98 } 99 100 } 101 | Popular Tags |