KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: Set.java,v 1.11 2005/06/01 04:25:05 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 import org.hibernate.type.CollectionType;
9 import org.hibernate.type.TypeFactory;
10
11 /**
12  * A set with no nullable element columns. It will have a primary key
13  * consisting of all table columns (ie. key columns + element columns).
14  * @author Gavin King
15  */

16 public class Set extends Collection {
17
18     public void validate(Mapping mapping) throws MappingException {
19         super.validate( mapping );
20         //for backward compatibility, disable this:
21
/*Iterator iter = getElement().getColumnIterator();
22         while ( iter.hasNext() ) {
23             Column col = (Column) iter.next();
24             if ( !col.isNullable() ) {
25                 return;
26             }
27         }
28         throw new MappingException("set element mappings must have at least one non-nullable column: " + getRole() );*/

29     }
30
31     /**
32      * Constructor for Set.
33      * @param owner
34      */

35     public Set(PersistentClass owner) {
36         super(owner);
37     }
38
39     public boolean isSet() {
40         return true;
41     }
42
43     public CollectionType getDefaultCollectionType() {
44         return isSorted() ?
45             TypeFactory.sortedSet( getRole(), getReferencedPropertyName(), isEmbedded(), getComparator() ) :
46             TypeFactory.set( getRole(), getReferencedPropertyName(), isEmbedded() );
47     }
48
49     void createPrimaryKey() {
50         if ( !isOneToMany() ) {
51             PrimaryKey pk = new PrimaryKey();
52             pk.addColumns( getKey().getColumnIterator() );
53             Iterator JavaDoc iter = getElement().getColumnIterator();
54             while ( iter.hasNext() ) {
55                 Object JavaDoc selectable = iter.next();
56                 if ( selectable instanceof Column ) {
57                     Column col = (Column) selectable;
58                     if ( !col.isNullable() ) {
59                         pk.addColumn(col);
60                     }
61                 }
62             }
63             if ( pk.getColumnSpan()==getKey().getColumnSpan() ) {
64                 //for backward compatibility, allow a set with no not-null
65
//element columns, using all columns in the row locater SQL
66
//TODO: create an implicit not null constraint on all cols?
67
}
68             else {
69                 getCollectionTable().setPrimaryKey(pk);
70             }
71         }
72         else {
73             //create an index on the key columns??
74
}
75     }
76
77     public Object JavaDoc accept(ValueVisitor visitor) {
78         return visitor.accept(this);
79     }
80 }
81
Popular Tags