~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/tuple/PropertyFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// $Id: PropertyFactory.java 10119 2006-07-14 00:09:19Z steve.ebersole@jboss.com $
 
2
package org.hibernate.tuple;
 
3
 
 
4
import java.lang.reflect.Constructor;
 
5
 
 
6
import org.hibernate.EntityMode;
 
7
import org.hibernate.mapping.PropertyGeneration;
 
8
import org.hibernate.engine.IdentifierValue;
 
9
import org.hibernate.engine.UnsavedValueFactory;
 
10
import org.hibernate.engine.VersionValue;
 
11
import org.hibernate.id.IdentifierGenerator;
 
12
import org.hibernate.mapping.KeyValue;
 
13
import org.hibernate.mapping.PersistentClass;
 
14
import org.hibernate.mapping.Property;
 
15
import org.hibernate.property.Getter;
 
16
import org.hibernate.property.PropertyAccessor;
 
17
import org.hibernate.property.PropertyAccessorFactory;
 
18
import org.hibernate.type.AssociationType;
 
19
import org.hibernate.type.Type;
 
20
import org.hibernate.type.VersionType;
 
21
import org.hibernate.util.ReflectHelper;
 
22
 
 
23
/**
 
24
 * Responsible for generation of runtime metamodel {@link Property} representations.
 
25
 * Makes distinction between identifier, version, and other (standard) properties.
 
26
 *
 
27
 * @author Steve Ebersole
 
28
 */
 
29
public class PropertyFactory {
 
30
 
 
31
        /**
 
32
         * Generates an IdentifierProperty representation of the for a given entity mapping.
 
33
         *
 
34
         * @param mappedEntity The mapping definition of the entity.
 
35
         * @param generator The identifier value generator to use for this identifier.
 
36
         * @return The appropriate IdentifierProperty definition.
 
37
         */
 
38
        public static IdentifierProperty buildIdentifierProperty(PersistentClass mappedEntity, IdentifierGenerator generator) {
 
39
 
 
40
                String mappedUnsavedValue = mappedEntity.getIdentifier().getNullValue();
 
41
                Type type = mappedEntity.getIdentifier().getType();
 
42
                Property property = mappedEntity.getIdentifierProperty();
 
43
                
 
44
                IdentifierValue unsavedValue = UnsavedValueFactory.getUnsavedIdentifierValue(
 
45
                                mappedUnsavedValue,
 
46
                                getGetter( property ),
 
47
                                type,
 
48
                                getConstructor(mappedEntity)
 
49
                        );
 
50
 
 
51
                if ( property == null ) {
 
52
                        // this is a virtual id property...
 
53
                        return new IdentifierProperty(
 
54
                                type,
 
55
                                        mappedEntity.hasEmbeddedIdentifier(),
 
56
                                        mappedEntity.hasIdentifierMapper(),
 
57
                                        unsavedValue,
 
58
                                        generator
 
59
                                );
 
60
                }
 
61
                else {
 
62
                        return new IdentifierProperty(
 
63
                                        property.getName(),
 
64
                                        property.getNodeName(),
 
65
                                        type,
 
66
                                        mappedEntity.hasEmbeddedIdentifier(),
 
67
                                        unsavedValue,
 
68
                                        generator
 
69
                                );
 
70
                }
 
71
        }
 
72
 
 
73
        /**
 
74
         * Generates a VersionProperty representation for an entity mapping given its
 
75
         * version mapping Property.
 
76
         *
 
77
         * @param property The version mapping Property.
 
78
         * @param lazyAvailable Is property lazy loading currently available.
 
79
         * @return The appropriate VersionProperty definition.
 
80
         */
 
81
        public static VersionProperty buildVersionProperty(Property property, boolean lazyAvailable) {
 
82
                String mappedUnsavedValue = ( (KeyValue) property.getValue() ).getNullValue();
 
83
                
 
84
                VersionValue unsavedValue = UnsavedValueFactory.getUnsavedVersionValue(
 
85
                                mappedUnsavedValue, 
 
86
                                getGetter( property ),
 
87
                                (VersionType) property.getType(),
 
88
                                getConstructor( property.getPersistentClass() )
 
89
                        );
 
90
 
 
91
                boolean lazy = lazyAvailable && property.isLazy();
 
92
 
 
93
                return new VersionProperty(
 
94
                        property.getName(),
 
95
                        property.getNodeName(),
 
96
                        property.getValue().getType(),
 
97
                        lazy,
 
98
                                property.isInsertable(),
 
99
                                property.isUpdateable(),
 
100
                        property.getGeneration() == PropertyGeneration.INSERT || property.getGeneration() == PropertyGeneration.ALWAYS,
 
101
                                property.getGeneration() == PropertyGeneration.ALWAYS,
 
102
                                property.isOptional(),
 
103
                                property.isUpdateable() && !lazy,
 
104
                                property.isOptimisticLocked(),
 
105
                        property.getCascadeStyle(),
 
106
                        unsavedValue
 
107
                        );
 
108
        }
 
109
 
 
110
        /**
 
111
         * Generate a "standard" (i.e., non-identifier and non-version) based on the given
 
112
         * mapped property.
 
113
         *
 
114
         * @param property The mapped property.
 
115
         * @param lazyAvailable Is property lazy loading currently available.
 
116
         * @return The appropriate StandardProperty definition.
 
117
         */
 
118
        public static StandardProperty buildStandardProperty(Property property, boolean lazyAvailable) {
 
119
                
 
120
                final Type type = property.getValue().getType();
 
121
                
 
122
                // we need to dirty check collections, since they can cause an owner
 
123
                // version number increment
 
124
                
 
125
                // we need to dirty check many-to-ones with not-found="ignore" in order 
 
126
                // to update the cache (not the database), since in this case a null
 
127
                // entity reference can lose information
 
128
                
 
129
                boolean alwaysDirtyCheck = type.isAssociationType() && 
 
130
                                ( (AssociationType) type ).isAlwaysDirtyChecked(); 
 
131
 
 
132
                return new StandardProperty(
 
133
                                property.getName(),
 
134
                                property.getNodeName(),
 
135
                                type,
 
136
                                lazyAvailable && property.isLazy(),
 
137
                                property.isInsertable(),
 
138
                                property.isUpdateable(),
 
139
                        property.getGeneration() == PropertyGeneration.INSERT || property.getGeneration() == PropertyGeneration.ALWAYS,
 
140
                                property.getGeneration() == PropertyGeneration.ALWAYS,
 
141
                                property.isOptional(),
 
142
                                alwaysDirtyCheck || property.isUpdateable(),
 
143
                                property.isOptimisticLocked(),
 
144
                                property.getCascadeStyle(),
 
145
                        property.getValue().getFetchMode()
 
146
                        );
 
147
        }
 
148
 
 
149
        private static Constructor getConstructor(PersistentClass persistentClass) {
 
150
                if ( persistentClass == null || !persistentClass.hasPojoRepresentation() ) {
 
151
                        return null;
 
152
                }
 
153
 
 
154
                try {
 
155
                        return ReflectHelper.getDefaultConstructor( persistentClass.getMappedClass() );
 
156
                }
 
157
                catch( Throwable t ) {
 
158
                        return null;
 
159
                }
 
160
        }
 
161
 
 
162
        private static Getter getGetter(Property mappingProperty) {
 
163
                if ( mappingProperty == null || !mappingProperty.getPersistentClass().hasPojoRepresentation() ) {
 
164
                        return null;
 
165
                }
 
166
 
 
167
                PropertyAccessor pa = PropertyAccessorFactory.getPropertyAccessor( mappingProperty, EntityMode.POJO );
 
168
                return pa.getGetter( mappingProperty.getPersistentClass().getMappedClass(), mappingProperty.getName() );
 
169
        }
 
170
 
 
171
}