KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > tuple > PropertyFactory


1 // $Id: PropertyFactory.java,v 1.7 2005/07/21 01:58:51 oneovthafew Exp $
2
package org.hibernate.tuple;
3
4 import java.lang.reflect.Constructor JavaDoc;
5
6 import org.hibernate.EntityMode;
7 import org.hibernate.engine.IdentifierValue;
8 import org.hibernate.engine.UnsavedValueFactory;
9 import org.hibernate.engine.VersionValue;
10 import org.hibernate.id.IdentifierGenerator;
11 import org.hibernate.mapping.KeyValue;
12 import org.hibernate.mapping.PersistentClass;
13 import org.hibernate.mapping.Property;
14 import org.hibernate.property.Getter;
15 import org.hibernate.property.PropertyAccessor;
16 import org.hibernate.property.PropertyAccessorFactory;
17 import org.hibernate.type.AssociationType;
18 import org.hibernate.type.Type;
19 import org.hibernate.type.VersionType;
20 import org.hibernate.util.ReflectHelper;
21
22 /**
23  * Responsible for generation of runtime metamodel {@link Property} representations.
24  * Makes distinction between identifier, version, and other (standard) properties.
25  *
26  * @author Steve Ebersole
27  */

28 public class PropertyFactory {
29
30     /**
31      * Generates an IdentifierProperty representation of the for a given entity mapping.
32      *
33      * @param mappedEntity The mapping definition of the entity.
34      * @param generator The identifier value generator to use for this identifier.
35      * @return The appropriate IdentifierProperty definition.
36      */

37     public static IdentifierProperty buildIdentifierProperty(PersistentClass mappedEntity, IdentifierGenerator generator) {
38
39         String JavaDoc mappedUnsavedValue = mappedEntity.getIdentifier().getNullValue();
40         Type type = mappedEntity.getIdentifier().getType();
41         Property property = mappedEntity.getIdentifierProperty();
42         
43         IdentifierValue unsavedValue = UnsavedValueFactory.getUnsavedIdentifierValue(
44                 mappedUnsavedValue,
45                 getGetter( property ),
46                 type,
47                 getConstructor(mappedEntity)
48             );
49
50         if ( property == null ) {
51             // this is a virtual id property...
52
return new IdentifierProperty(
53                     type,
54                     mappedEntity.hasEmbeddedIdentifier(),
55                     unsavedValue,
56                     generator
57                 );
58         }
59         else {
60             return new IdentifierProperty(
61                     property.getName(),
62                     property.getNodeName(),
63                     type,
64                     mappedEntity.hasEmbeddedIdentifier(),
65                     unsavedValue,
66                     generator
67                 );
68         }
69     }
70
71     /**
72      * Generates a VersionProperty representation for an entity mapping given its
73      * version mapping Property.
74      *
75      * @param property The version mapping Property.
76      * @param lazyAvailable Is property lazy loading currently available.
77      * @return The appropriate VersionProperty definition.
78      */

79     public static VersionProperty buildVersionProperty(Property property, boolean lazyAvailable) {
80         String JavaDoc mappedUnsavedValue = ( (KeyValue) property.getValue() ).getNullValue();
81         
82         VersionValue unsavedValue = UnsavedValueFactory.getUnsavedVersionValue(
83                 mappedUnsavedValue,
84                 getGetter( property ),
85                 (VersionType) property.getType(),
86                 getConstructor( property.getPersistentClass() )
87             );
88
89         boolean lazy = lazyAvailable && property.isLazy();
90
91         return new VersionProperty(
92                 property.getName(),
93                 property.getNodeName(),
94                 property.getValue().getType(),
95                 lazy,
96                 property.isInsertable(),
97                 property.isUpdateable(),
98                 property.isOptional(),
99                 property.isUpdateable() && !lazy,
100                 property.isOptimisticLocked(),
101                 property.getCascadeStyle(),
102                 unsavedValue
103             );
104     }
105
106     /**
107      * Generate a "standard" (i.e., non-identifier and non-version) based on the given
108      * mapped property.
109      *
110      * @param property The mapped property.
111      * @param lazyAvailable Is property lazy loading currently available.
112      * @return The appropriate StandardProperty definition.
113      */

114     public static StandardProperty buildStandardProperty(Property property, boolean lazyAvailable) {
115         
116         final Type type = property.getValue().getType();
117         
118         // we need to dirty check collections, since they can cause an owner
119
// version number increment
120

121         // we need to dirty check many-to-ones with not-found="ignore" in order
122
// to update the cache (not the database), since in this case a null
123
// entity reference can lose information
124

125         boolean alwaysDirtyCheck = type.isAssociationType() &&
126                 ( (AssociationType) type ).isAlwaysDirtyChecked();
127
128         return new StandardProperty(
129                 property.getName(),
130                 property.getNodeName(),
131                 type,
132                 lazyAvailable && property.isLazy(),
133                 property.isInsertable(),
134                 property.isUpdateable(),
135                 property.isOptional(),
136                 alwaysDirtyCheck || property.isUpdateable(),
137                 property.isOptimisticLocked(),
138                 property.getCascadeStyle()
139             );
140     }
141
142     private static Constructor JavaDoc getConstructor(PersistentClass persistentClass) {
143         if ( persistentClass == null || !persistentClass.hasPojoRepresentation() ) {
144             return null;
145         }
146
147         try {
148             return ReflectHelper.getDefaultConstructor( persistentClass.getMappedClass() );
149         }
150         catch( Throwable JavaDoc t ) {
151             return null;
152         }
153     }
154
155     private static Getter getGetter(Property mappingProperty) {
156         if ( mappingProperty == null || !mappingProperty.getPersistentClass().hasPojoRepresentation() ) {
157             return null;
158         }
159
160         PropertyAccessor pa = PropertyAccessorFactory.getPropertyAccessor( mappingProperty, EntityMode.POJO );
161         return pa.getGetter( mappingProperty.getPersistentClass().getMappedClass(), mappingProperty.getName() );
162     }
163
164 }
165
Popular Tags