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 import org.hibernate.type.CollectionType;
32 import org.hibernate.type.TypeFactory;
33
34 /**
35 * A set with no nullable element columns. It will have a primary key
36 * consisting of all table columns (ie. key columns + element columns).
37 * @author Gavin King
38 */
39 public class Set extends Collection {
40
41 public void validate(Mapping mapping) throws MappingException {
42 super.validate( mapping );
43 //for backward compatibility, disable this:
44 /*Iterator iter = getElement().getColumnIterator();
45 while ( iter.hasNext() ) {
46 Column col = (Column) iter.next();
47 if ( !col.isNullable() ) {
48 return;
49 }
50 }
51 throw new MappingException("set element mappings must have at least one non-nullable column: " + getRole() );*/
52 }
53
54 /**
55 * Constructor for Set.
56 * @param owner
57 */
58 public Set(PersistentClass owner) {
59 super(owner);
60 }
61
62 public boolean isSet() {
63 return true;
64 }
65
66 public CollectionType getDefaultCollectionType() {
67 if ( isSorted() ) {
68 return TypeFactory.sortedSet( getRole(), getReferencedPropertyName(), isEmbedded(), getComparator() );
69 }
70 else if ( hasOrder() ) {
71 return TypeFactory.orderedSet( getRole(), getReferencedPropertyName(), isEmbedded() );
72 }
73 else {
74 return TypeFactory.set( getRole(), getReferencedPropertyName(), isEmbedded() );
75 }
76 }
77
78 void createPrimaryKey() {
79 if ( !isOneToMany() ) {
80 PrimaryKey pk = new PrimaryKey();
81 pk.addColumns( getKey().getColumnIterator() );
82 Iterator iter = getElement().getColumnIterator();
83 while ( iter.hasNext() ) {
84 Object selectable = iter.next();
85 if ( selectable instanceof Column ) {
86 Column col = (Column) selectable;
87 if ( !col.isNullable() ) {
88 pk.addColumn(col);
89 }
90 }
91 }
92 if ( pk.getColumnSpan()==getKey().getColumnSpan() ) {
93 //for backward compatibility, allow a set with no not-null
94 //element columns, using all columns in the row locater SQL
95 //TODO: create an implicit not null constraint on all cols?
96 }
97 else {
98 getCollectionTable().setPrimaryKey(pk);
99 }
100 }
101 else {
102 //create an index on the key columns??
103 }
104 }
105
106 public Object accept(ValueVisitor visitor) {
107 return visitor.accept(this);
108 }
109 }