KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > persister > collection > CollectionPropertyMapping


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 /**
9  * @author Gavin King
10  */

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 JavaDoc 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             //return memberPersister.getPropertyType(propertyName);
44
throw new QueryException("illegal syntax near collection: " + propertyName);
45         }
46     }
47
48     public String JavaDoc[] toColumns(String JavaDoc alias, String JavaDoc 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 JavaDoc[] cols = memberPersister.getKeyColumnNames();
58             return new String JavaDoc[] { "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 JavaDoc[] cols = memberPersister.getIndexColumnNames(alias);
63             if ( cols.length!=1 ) throw new QueryException("composite collection index in maxIndex()");
64             return new String JavaDoc[] { "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 JavaDoc[] cols = memberPersister.getIndexColumnNames(alias);
69             if ( cols.length!=1 ) throw new QueryException("composite collection index in minIndex()");
70             return new String JavaDoc[] { "min(" + cols[0] + ')' };
71         }
72         else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_MAX_ELEMENT) ) {
73             String JavaDoc[] cols = memberPersister.getElementColumnNames(alias);
74             if ( cols.length!=1 ) throw new QueryException("composite collection element in maxElement()");
75             return new String JavaDoc[] { "max(" + cols[0] + ')' };
76         }
77         else if ( propertyName.equals(CollectionPropertyNames.COLLECTION_MIN_ELEMENT) ) {
78             String JavaDoc[] cols = memberPersister.getElementColumnNames(alias);
79             if ( cols.length!=1 ) throw new QueryException("composite collection element in minElement()");
80             return new String JavaDoc[] { "min(" + cols[0] + ')' };
81         }
82         else {
83             //return memberPersister.toColumns(alias, propertyName);
84
throw new QueryException("illegal syntax near collection: " + propertyName);
85         }
86     }
87
88     /**
89      * Given a property path, return the corresponding column name(s).
90      */

91     public String JavaDoc[] toColumns(String JavaDoc propertyName) throws QueryException, UnsupportedOperationException JavaDoc {
92         throw new UnsupportedOperationException JavaDoc( "References to collections must be define a SQL alias" );
93     }
94
95     public Type getType() {
96         //return memberPersister.getType();
97
return memberPersister.getCollectionType();
98     }
99
100 }
101
Popular Tags