/
IndexedCollection.java
91 lines (78 loc) · 2.3 KB
/
IndexedCollection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.mapping;
import java.util.Iterator;
import org.hibernate.MappingException;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.engine.spi.Mapping;
/**
* Indexed collections include Lists, Maps, arrays and
* primitive arrays.
* @author Gavin King
*/
public abstract class IndexedCollection extends Collection {
public static final String DEFAULT_INDEX_COLUMN_NAME = "idx";
private Value index;
public IndexedCollection(MetadataImplementor metadata, PersistentClass owner) {
super( metadata, owner );
}
public Value getIndex() {
return index;
}
public void setIndex(Value index) {
this.index = index;
}
public final boolean isIndexed() {
return true;
}
void createPrimaryKey() {
if ( !isOneToMany() ) {
PrimaryKey pk = new PrimaryKey();
pk.addColumns( getKey().getColumnIterator() );
// index should be last column listed
boolean isFormula = false;
Iterator iter = getIndex().getColumnIterator();
while ( iter.hasNext() ) {
if ( ( (Selectable) iter.next() ).isFormula() ) {
isFormula=true;
}
}
if (isFormula) {
//if it is a formula index, use the element columns in the PK
pk.addColumns( getElement().getColumnIterator() );
}
else {
pk.addColumns( getIndex().getColumnIterator() );
}
getCollectionTable().setPrimaryKey(pk);
}
else {
// don't create a unique key, 'cos some
// databases don't like a UK on nullable
// columns
/*ArrayList list = new ArrayList();
list.addAll( getKey().getConstraintColumns() );
list.addAll( getIndex().getConstraintColumns() );
getCollectionTable().createUniqueKey(list);*/
}
}
public void validate(Mapping mapping) throws MappingException {
super.validate( mapping );
assert getElement() != null : "IndexedCollection index not bound : " + getRole();
if ( !getIndex().isValid(mapping) ) {
throw new MappingException(
"collection index mapping has wrong number of columns: " +
getRole() +
" type: " +
getIndex().getType().getName()
);
}
}
public boolean isList() {
return false;
}
}