1 package org.hibernate.tuple; 3 4 import java.lang.reflect.Constructor ; 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 28 public class PropertyFactory { 29 30 37 public static IdentifierProperty buildIdentifierProperty(PersistentClass mappedEntity, IdentifierGenerator generator) { 38 39 String 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 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 79 public static VersionProperty buildVersionProperty(Property property, boolean lazyAvailable) { 80 String 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 114 public static StandardProperty buildStandardProperty(Property property, boolean lazyAvailable) { 115 116 final Type type = property.getValue().getType(); 117 118 121 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 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 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 |