1 package org.hibernate.persister.entity; 3 4 import java.util.HashMap ; 5 import java.util.Map ; 6 7 import org.hibernate.MappingException; 8 import org.hibernate.QueryException; 9 import org.hibernate.engine.Mapping; 10 import org.hibernate.sql.Template; 11 import org.hibernate.type.AbstractComponentType; 12 import org.hibernate.type.AssociationType; 13 import org.hibernate.type.EntityType; 14 import org.hibernate.type.Type; 15 import org.hibernate.util.ArrayHelper; 16 import org.hibernate.util.StringHelper; 17 18 23 public abstract class AbstractPropertyMapping implements PropertyMapping { 24 25 private final Map typesByPropertyPath = new HashMap (); 26 private final Map columnsByPropertyPath = new HashMap (); 27 private final Map formulaTemplatesByPropertyPath = new HashMap (); 28 29 public String [] getIdentifierColumnNames() { 30 throw new UnsupportedOperationException ("one-to-one is not supported here"); 31 } 32 33 protected abstract String getEntityName(); 34 35 public Type toType(String propertyName) throws QueryException { 36 Type type = (Type) typesByPropertyPath.get(propertyName); 37 if (type==null) throwPropertyException(propertyName); 38 return type; 39 } 40 41 protected final void throwPropertyException(String propertyName) 42 throws QueryException { 43 throw new QueryException( 44 "could not resolve property: " + 45 propertyName + 46 " of: " + 47 getEntityName() 48 ); 49 } 50 51 public String [] getColumnNames(String propertyName) { 52 String [] cols = (String []) columnsByPropertyPath.get(propertyName); 53 if (cols==null) { 54 throw new MappingException("unknown property: " + propertyName); 55 } 56 return cols; 57 } 58 59 public String [] toColumns(String alias, String propertyName) 60 throws QueryException { 61 String [] columns = (String []) columnsByPropertyPath.get(propertyName); 63 if (columns==null) throwPropertyException(propertyName); 64 String [] templates = (String []) formulaTemplatesByPropertyPath.get(propertyName); 65 String [] result = new String [columns.length]; 66 for ( int i=0; i<columns.length; i++ ) { 67 if ( columns[i]==null ) { 68 result[i] = StringHelper.replace( templates[i], Template.TEMPLATE, alias ); 69 } 70 else { 71 result[i] = StringHelper.qualify( alias, columns[i] ); 72 } 73 } 74 return result; 75 } 76 77 public String [] toColumns(String propertyName) 78 throws QueryException { 79 String [] columns = (String []) columnsByPropertyPath.get(propertyName); 80 if (columns==null) throwPropertyException(propertyName); 81 String [] templates = (String []) formulaTemplatesByPropertyPath.get(propertyName); 82 String [] result = new String [columns.length]; 83 for ( int i=0; i<columns.length; i++ ) { 84 if ( columns[i]==null ) { 85 result[i] = StringHelper.replace( templates[i], Template.TEMPLATE, "" ); 86 } 87 else { 88 result[i] = columns[i]; 89 } 90 } 91 return result; 92 } 93 94 protected void addPropertyPath(String path, Type type, String [] columns, String [] formulaTemplates) { 95 typesByPropertyPath.put(path, type); 96 columnsByPropertyPath.put(path, columns); 97 if (formulaTemplates!=null) { 98 formulaTemplatesByPropertyPath.put(path, formulaTemplates); 99 } 100 } 101 102 112 113 protected void initPropertyPaths( 114 final String path, 115 final Type type, 116 String [] columns, 117 String [] formulaTemplates, final Mapping factory) 118 throws MappingException { 119 120 if ( columns.length!=type.getColumnSpan(factory) ) { 121 throw new MappingException( "broken column mapping for: " + path + " of: " + getEntityName() ); 122 } 123 124 if ( type.isAssociationType() ) { 125 AssociationType actype = (AssociationType) type; 126 if ( actype.useLHSPrimaryKey() ) { 127 columns = getIdentifierColumnNames(); 128 } 129 else { 130 String foreignKeyProperty = actype.getLHSPropertyName(); 131 if ( foreignKeyProperty!=null && !path.equals(foreignKeyProperty) ) { 132 columns = (String []) columnsByPropertyPath.get(foreignKeyProperty); 135 if (columns==null) return; } 137 } 138 } 139 140 if (path!=null) addPropertyPath(path, type, columns, formulaTemplates); 141 142 if ( type.isComponentType() ) { 143 initComponentPropertyPaths( path, (AbstractComponentType) type, columns, formulaTemplates, factory ); 144 } 145 else if ( type.isEntityType() ) { 146 initIdentifierPropertyPaths( path, (EntityType) type, columns, factory ); 147 } 148 } 149 150 protected void initIdentifierPropertyPaths( 151 final String path, 152 final EntityType etype, 153 final String [] columns, 154 final Mapping factory) 155 throws MappingException { 156 157 Type idtype = etype.getIdentifierOrUniqueKeyType(factory); 158 159 if ( etype.isReferenceToPrimaryKey() ) { 160 String idpath1 = extendPath(path, EntityPersister.ENTITY_ID); 161 addPropertyPath(idpath1, idtype, columns, null); 162 initPropertyPaths(idpath1, idtype, columns, null, factory); 163 } 164 165 String idPropName = etype.getIdentifierOrUniqueKeyPropertyName(factory); 166 if (idPropName!=null) { 167 String idpath2 = extendPath(path, idPropName); 168 addPropertyPath(idpath2, idtype, columns, null); 169 initPropertyPaths(idpath2, idtype, columns, null, factory); 170 } 171 } 172 173 protected void initComponentPropertyPaths( 174 final String path, 175 final AbstractComponentType type, 176 final String [] columns, 177 String [] formulaTemplates, final Mapping factory) 178 throws MappingException { 179 180 Type[] types = type.getSubtypes(); 181 String [] properties = type.getPropertyNames(); 182 int begin=0; 183 for ( int i=0; i<properties.length; i++ ) { 184 String subpath = extendPath( path, properties[i] ); 185 try { 186 int length = types[i].getColumnSpan(factory); 187 String [] columnSlice = ArrayHelper.slice(columns, begin, length); 188 String [] formulaSlice = formulaTemplates==null ? 189 null : ArrayHelper.slice(formulaTemplates, begin, length); 190 initPropertyPaths(subpath, types[i], columnSlice, formulaSlice, factory); 191 begin+=length; 192 } 193 catch (Exception e) { 194 throw new MappingException("bug in initComponentPropertyPaths", e); 195 } 196 } 197 } 198 199 private static String extendPath(String path, String property) { 200 if (path==null) { 201 return property; 202 } 203 else { 204 return StringHelper.qualify(path, property); 205 } 206 } 207 208 } 209 | Popular Tags |