1 /*
2 * Hibernate, Relational Persistence for Idiomatic Java
3 *
4 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5 * indicated by the @author tags or express copyright attribution
6 * statements applied by the authors. All third-party contributions are
7 * distributed under license by Red Hat Middleware LLC.
8 *
9 * This copyrighted material is made available to anyone wishing to use, modify,
10 * copy, or redistribute it subject to the terms and conditions of the GNU
11 * Lesser General Public License, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this distribution; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301 USA
23 *
24 */
25 package org.hibernate.mapping;
26
27 import java.util.Iterator;
28
29 import org.hibernate.MappingException;
30 import org.hibernate.engine.Mapping;
31
32 /**
33 * Indexed collections include Lists, Maps, arrays and
34 * primitive arrays.
35 * @author Gavin King
36 */
37 public abstract class IndexedCollection extends Collection {
38
39 public static final String DEFAULT_INDEX_COLUMN_NAME = "idx";
40
41 private Value index;
42 private String indexNodeName;
43
44 public IndexedCollection(PersistentClass owner) {
45 super(owner);
46 }
47
48 public Value getIndex() {
49 return index;
50 }
51 public void setIndex(Value index) {
52 this.index = index;
53 }
54 public final boolean isIndexed() {
55 return true;
56 }
57
58 void createPrimaryKey() {
59 if ( !isOneToMany() ) {
60 PrimaryKey pk = new PrimaryKey();
61 pk.addColumns( getKey().getColumnIterator() );
62
63 // index should be last column listed
64 boolean isFormula = false;
65 Iterator iter = getIndex().getColumnIterator();
66 while ( iter.hasNext() ) {
67 if ( ( (Selectable) iter.next() ).isFormula() ) isFormula=true;
68 }
69 if (isFormula) {
70 //if it is a formula index, use the element columns in the PK
71 pk.addColumns( getElement().getColumnIterator() );
72 }
73 else {
74 pk.addColumns( getIndex().getColumnIterator() );
75 }
76 getCollectionTable().setPrimaryKey(pk);
77 }
78 else {
79 // don't create a unique key, 'cos some
80 // databases don't like a UK on nullable
81 // columns
82 /*ArrayList list = new ArrayList();
83 list.addAll( getKey().getConstraintColumns() );
84 list.addAll( getIndex().getConstraintColumns() );
85 getCollectionTable().createUniqueKey(list);*/
86 }
87 }
88
89 public void validate(Mapping mapping) throws MappingException {
90 super.validate(mapping);
91 if ( !getIndex().isValid(mapping) ) {
92 throw new MappingException(
93 "collection index mapping has wrong number of columns: " +
94 getRole() +
95 " type: " +
96 getIndex().getType().getName()
97 );
98 }
99 if ( indexNodeName!=null && !indexNodeName.startsWith("@") ) {
100 throw new MappingException("index node must be an attribute: " + indexNodeName );
101 }
102 }
103
104 public boolean isList() {
105 return false;
106 }
107
108 public String getIndexNodeName() {
109 return indexNodeName;
110 }
111
112 public void setIndexNodeName(String indexNodeName) {
113 this.indexNodeName = indexNodeName;
114 }
115
116
117 }