KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: RootClass.java,v 1.14 2005/05/31 20:39:41 oneovthafew Exp $
2
package org.hibernate.mapping;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.Set JavaDoc;
8
9 import org.apache.commons.logging.LogFactory;
10 import org.hibernate.MappingException;
11 import org.hibernate.engine.Mapping;
12 import org.hibernate.util.ReflectHelper;
13 import org.hibernate.util.SingletonIterator;
14
15 /**
16  * The root class of an inheritance hierarchy
17  * @author Gavin King
18  */

19 public class RootClass extends PersistentClass implements TableOwner {
20
21     public static final String JavaDoc DEFAULT_IDENTIFIER_COLUMN_NAME = "id";
22     public static final String JavaDoc DEFAULT_DISCRIMINATOR_COLUMN_NAME = "class";
23
24     private Property identifierProperty; //may be final
25
private KeyValue identifier; //may be final
26
private Property version; //may be final
27
private boolean polymorphic;
28     private String JavaDoc cacheConcurrencyStrategy;
29     private String JavaDoc cacheRegionName;
30     private Value discriminator; //may be final
31
private boolean mutable = true;
32     private boolean embeddedIdentifier = false; // may be final
33
private boolean explicitPolymorphism;
34     private Class JavaDoc entityPersisterClass;
35     private boolean forceDiscriminator = false;
36     private String JavaDoc where;
37     private Table table;
38     private boolean discriminatorInsertable = true;
39     private int nextSubclassId = 0;
40     
41     int nextSubclassId() {
42         return ++nextSubclassId;
43     }
44
45     public int getSubclassId() {
46         return 0;
47     }
48     
49     public void setTable(Table table) {
50         this.table=table;
51     }
52     public Table getTable() {
53         return table;
54     }
55
56     public Property getIdentifierProperty() {
57         return identifierProperty;
58     }
59     public KeyValue getIdentifier() {
60         return identifier;
61     }
62     public boolean hasIdentifierProperty() {
63         return identifierProperty!=null;
64     }
65
66     public Value getDiscriminator() {
67         return discriminator;
68     }
69
70     public boolean isInherited() {
71         return false;
72     }
73     public boolean isPolymorphic() {
74         return polymorphic;
75     }
76
77     public void setPolymorphic(boolean polymorphic) {
78         this.polymorphic = polymorphic;
79     }
80
81     public RootClass getRootClass() {
82         return this;
83     }
84
85     public Iterator JavaDoc getPropertyClosureIterator() {
86         return getPropertyIterator();
87     }
88     public Iterator JavaDoc getTableClosureIterator() {
89         return new SingletonIterator( getTable() );
90     }
91     public Iterator JavaDoc getKeyClosureIterator() {
92         return new SingletonIterator( getKey() );
93     }
94
95     public void addSubclass(Subclass subclass) throws MappingException {
96         super.addSubclass(subclass);
97         setPolymorphic(true);
98     }
99
100     public boolean isExplicitPolymorphism() {
101         return explicitPolymorphism;
102     }
103
104     public Property getVersion() {
105         return version;
106     }
107     public void setVersion(Property version) {
108         this.version = version;
109     }
110     public boolean isVersioned() {
111         return version!=null;
112     }
113
114     public boolean isMutable() {
115         return mutable;
116     }
117     public boolean hasEmbeddedIdentifier() {
118         return embeddedIdentifier;
119     }
120
121     public Class JavaDoc getEntityPersisterClass() {
122         return entityPersisterClass;
123     }
124
125     public Table getRootTable() {
126         return getTable();
127     }
128
129     public void setEntityPersisterClass(Class JavaDoc persister) {
130         this.entityPersisterClass = persister;
131     }
132
133     public PersistentClass getSuperclass() {
134         return null;
135     }
136
137     public KeyValue getKey() {
138         return getIdentifier();
139     }
140
141     public void setDiscriminator(Value discriminator) {
142         this.discriminator = discriminator;
143     }
144
145     public void setEmbeddedIdentifier(boolean embeddedIdentifier) {
146         this.embeddedIdentifier = embeddedIdentifier;
147     }
148
149     public void setExplicitPolymorphism(boolean explicitPolymorphism) {
150         this.explicitPolymorphism = explicitPolymorphism;
151     }
152
153     public void setIdentifier(KeyValue identifier) {
154         this.identifier = identifier;
155     }
156
157     public void setIdentifierProperty(Property identifierProperty) {
158         this.identifierProperty = identifierProperty;
159         identifierProperty.setPersistentClass(this);
160     }
161
162     public void setMutable(boolean mutable) {
163         this.mutable = mutable;
164     }
165
166     public boolean isDiscriminatorInsertable() {
167         return discriminatorInsertable;
168     }
169     
170     public void setDiscriminatorInsertable(boolean insertable) {
171         this.discriminatorInsertable = insertable;
172     }
173
174     public boolean isForceDiscriminator() {
175         return forceDiscriminator;
176     }
177
178     public void setForceDiscriminator(boolean forceDiscriminator) {
179         this.forceDiscriminator = forceDiscriminator;
180     }
181
182     public String JavaDoc getWhere() {
183         return where;
184     }
185
186     public void setWhere(String JavaDoc string) {
187         where = string;
188     }
189
190     public void validate(Mapping mapping) throws MappingException {
191         super.validate(mapping);
192         if ( !getIdentifier().isValid(mapping) ) {
193             throw new MappingException(
194                 "identifier mapping has wrong number of columns: " +
195                 getEntityName() +
196                 " type: " +
197                 getIdentifier().getType().getName()
198             );
199         }
200         checkCompositeIdentifier();
201     }
202
203     private void checkCompositeIdentifier() {
204         if ( getIdentifier() instanceof Component ) {
205             Component id = (Component) getIdentifier();
206             if ( !id.isDynamic() ) {
207                 Class JavaDoc idClass = id.getComponentClass();
208                 if ( idClass != null && !ReflectHelper.overridesEquals( idClass ) ) {
209                     LogFactory.getLog(RootClass.class)
210                         .warn( "composite-id class does not override equals(): "
211                             + id.getComponentClass().getName() );
212                 }
213                 if ( !ReflectHelper.overridesHashCode( idClass ) ) {
214                     LogFactory.getLog(RootClass.class)
215                         .warn( "composite-id class does not override hashCode(): "
216                             + id.getComponentClass().getName() );
217                 }
218                 if ( !Serializable JavaDoc.class.isAssignableFrom( idClass ) ) {
219                     throw new MappingException( "composite-id class must implement Serializable: "
220                         + id.getComponentClass().getName() );
221                 }
222             }
223         }
224     }
225     
226     public String JavaDoc getCacheConcurrencyStrategy() {
227         return cacheConcurrencyStrategy;
228     }
229
230     public void setCacheConcurrencyStrategy(String JavaDoc cacheConcurrencyStrategy) {
231         this.cacheConcurrencyStrategy = cacheConcurrencyStrategy;
232     }
233
234     public String JavaDoc getCacheRegionName() {
235         return cacheRegionName==null ? getEntityName() : cacheRegionName;
236     }
237     public void setCacheRegionName(String JavaDoc cacheRegionName) {
238         this.cacheRegionName = cacheRegionName;
239     }
240
241     public boolean isJoinedSubclass() {
242         return false;
243     }
244
245     public java.util.Set JavaDoc getSynchronizedTables() {
246         return synchronizedTables;
247     }
248     
249     public Set getIdentityTables() {
250         Set tables = new HashSet JavaDoc();
251         Iterator JavaDoc iter = getSubclassClosureIterator();
252         while ( iter.hasNext() ) {
253             PersistentClass clazz = (PersistentClass) iter.next();
254             if ( !clazz.isAbstract() ) tables.add( clazz.getIdentityTable() );
255         }
256         return tables;
257     }
258     
259     public Object JavaDoc accept(PersistentClassVisitor mv) {
260         return mv.accept(this);
261     }
262 }
263
264
265
266
267
268
269
Popular Tags