KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > mapping > IndexedCollection


1 //$Id: IndexedCollection.java,v 1.7 2005/02/20 08:37:55 oneovthafew Exp $
2
package org.hibernate.mapping;
3
4 import java.util.Iterator JavaDoc;
5
6 import org.hibernate.MappingException;
7 import org.hibernate.engine.Mapping;
8
9 /**
10  * Indexed collections include Lists, Maps, arrays and
11  * primitive arrays.
12  * @author Gavin King
13  */

14 public abstract class IndexedCollection extends Collection {
15
16     public static final String JavaDoc DEFAULT_INDEX_COLUMN_NAME = "idx";
17
18     private Value index;
19     private String JavaDoc indexNodeName;
20
21     public IndexedCollection(PersistentClass owner) {
22         super(owner);
23     }
24
25     public Value getIndex() {
26         return index;
27     }
28     public void setIndex(Value index) {
29         this.index = index;
30     }
31     public final boolean isIndexed() {
32         return true;
33     }
34
35     void createPrimaryKey() {
36         if ( !isOneToMany() ) {
37             PrimaryKey pk = new PrimaryKey();
38             pk.addColumns( getKey().getColumnIterator() );
39             
40             // index should be last column listed
41
boolean isFormula = false;
42             Iterator JavaDoc iter = getIndex().getColumnIterator();
43             while ( iter.hasNext() ) {
44                 if ( ( (Selectable) iter.next() ).isFormula() ) isFormula=true;
45             }
46             if (isFormula) {
47                 //if it is a formula index, use the element columns in the PK
48
pk.addColumns( getElement().getColumnIterator() );
49             }
50             else {
51                 pk.addColumns( getIndex().getColumnIterator() );
52             }
53             getCollectionTable().setPrimaryKey(pk);
54         }
55         else {
56             // don't create a unique key, 'cos some
57
// databases don't like a UK on nullable
58
// columns
59
/*ArrayList list = new ArrayList();
60             list.addAll( getKey().getConstraintColumns() );
61             list.addAll( getIndex().getConstraintColumns() );
62             getCollectionTable().createUniqueKey(list);*/

63         }
64     }
65
66     public void validate(Mapping mapping) throws MappingException {
67         super.validate(mapping);
68         if ( !getIndex().isValid(mapping) ) {
69             throw new MappingException(
70                 "collection index mapping has wrong number of columns: " +
71                 getRole() +
72                 " type: " +
73                 getIndex().getType().getName()
74             );
75         }
76         if ( indexNodeName!=null && !indexNodeName.startsWith("@") ) {
77             throw new MappingException("index node must be an attribute: " + indexNodeName );
78         }
79     }
80     
81     public boolean isList() {
82         return false;
83     }
84
85     public String JavaDoc getIndexNodeName() {
86         return indexNodeName;
87     }
88
89     public void setIndexNodeName(String JavaDoc indexNodeName) {
90         this.indexNodeName = indexNodeName;
91     }
92     
93
94 }
95
Popular Tags