1 package org.hibernate.mapping; 3 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Properties ; 8 9 import org.hibernate.FetchMode; 10 import org.hibernate.MappingException; 11 import org.hibernate.dialect.Dialect; 12 import org.hibernate.engine.Mapping; 13 import org.hibernate.id.IdentifierGenerator; 14 import org.hibernate.id.IdentifierGeneratorFactory; 15 import org.hibernate.id.IdentityGenerator; 16 import org.hibernate.id.PersistentIdentifierGenerator; 17 import org.hibernate.type.Type; 18 import org.hibernate.type.TypeFactory; 19 import org.hibernate.util.ReflectHelper; 20 21 25 public class SimpleValue implements KeyValue { 26 27 private final List columns = new ArrayList (); 28 private String typeName; 29 private Properties identifierGeneratorProperties; 30 private String identifierGeneratorStrategy = "assigned"; 31 private String nullValue; 32 private Table table; 33 private String foreignKeyName; 34 private boolean alternateUniqueKey; 35 private Properties typeParameters; 36 private boolean cascadeDeleteEnabled; 37 38 public boolean isCascadeDeleteEnabled() { 39 return cascadeDeleteEnabled; 40 } 41 42 public void setCascadeDeleteEnabled(boolean cascadeDeleteEnabled) { 43 this.cascadeDeleteEnabled = cascadeDeleteEnabled; 44 } 45 46 public void addColumn(Column column) { 47 if ( !columns.contains(column) ) columns.add(column); 48 column.setValue(this); 49 column.setTypeIndex( columns.size()-1 ); 50 } 51 52 public void addFormula(Formula formula) { 53 columns.add(formula); 54 } 55 56 public boolean hasFormula() { 57 Iterator iter = getColumnIterator(); 58 while ( iter.hasNext() ) { 59 Object o = iter.next(); 60 if (o instanceof Formula) return true; 61 } 62 return false; 63 } 64 65 public int getColumnSpan() { 66 return columns.size(); 67 } 68 public Iterator getColumnIterator() { 69 return columns.iterator(); 70 } 71 public List getConstraintColumns() { 72 return columns; 73 } 74 public String getTypeName() { 75 return typeName; 76 } 77 public void setTypeName(String type) { 78 this.typeName = type; 79 } 80 public void setTable(Table table) { 81 this.table = table; 82 } 83 84 public SimpleValue(Table table) { 85 this.table = table; 86 } 87 88 public SimpleValue() {} 89 90 public void createForeignKey() throws MappingException {} 91 92 public void createForeignKeyOfEntity(String entityName) { 93 if ( !hasFormula() && !"none".equals(getForeignKeyName())) { 94 ForeignKey fk = table.createForeignKey( getForeignKeyName(), getConstraintColumns(), entityName ); 95 fk.setCascadeDeleteEnabled(cascadeDeleteEnabled); 96 } 97 } 98 99 public IdentifierGenerator createIdentifierGenerator( 100 Dialect dialect, 101 String defaultCatalog, 102 String defaultSchema, 103 RootClass rootClass) 104 throws MappingException { 105 106 Properties params = new Properties (); 107 108 if ( defaultSchema!=null ) { 113 params.setProperty(PersistentIdentifierGenerator.SCHEMA, defaultSchema); 114 } 115 if ( defaultCatalog!=null ) { 116 params.setProperty(PersistentIdentifierGenerator.CATALOG, defaultCatalog); 117 } 118 119 if (rootClass!=null) { 121 params.setProperty( IdentifierGenerator.ENTITY_NAME, rootClass.getEntityName() ); 122 } 123 124 String tableName = getTable().getQuotedName(dialect); 128 params.setProperty( PersistentIdentifierGenerator.TABLE, tableName ); 129 130 params.setProperty( PersistentIdentifierGenerator.PK, ( (Column) getColumnIterator().next() ).getName() ); 132 133 if (rootClass!=null) { 134 StringBuffer tables = new StringBuffer (); 135 Iterator iter = rootClass.getIdentityTables().iterator(); 136 while ( iter.hasNext() ) { 137 Table table= (Table) iter.next(); 138 tables.append( table.getQuotedName(dialect) ); 139 if ( iter.hasNext() ) tables.append(", "); 140 } 141 params.setProperty( PersistentIdentifierGenerator.TABLES, tables.toString() ); 142 } 143 else { 144 params.setProperty( PersistentIdentifierGenerator.TABLES, tableName ); 145 } 146 147 if (identifierGeneratorProperties!=null) { 148 params.putAll(identifierGeneratorProperties); 149 } 150 151 return IdentifierGeneratorFactory.create( 152 identifierGeneratorStrategy, 153 getType(), 154 params, 155 dialect 156 ); 157 158 } 159 160 public boolean isUpdateable() { 161 return true; 163 } 164 165 public FetchMode getFetchMode() { 166 return FetchMode.SELECT; 167 } 168 169 public Properties getIdentifierGeneratorProperties() { 170 return identifierGeneratorProperties; 171 } 172 173 public String getNullValue() { 174 return nullValue; 175 } 176 177 public Table getTable() { 178 return table; 179 } 180 181 185 public String getIdentifierGeneratorStrategy() { 186 return identifierGeneratorStrategy; 187 } 188 189 public boolean isIdentityColumn(Dialect dialect) { 190 return IdentifierGeneratorFactory.getIdentifierGeneratorClass(identifierGeneratorStrategy, dialect) 191 .equals(IdentityGenerator.class); 192 } 193 194 198 public void setIdentifierGeneratorProperties(Properties identifierGeneratorProperties) { 199 this.identifierGeneratorProperties = identifierGeneratorProperties; 200 } 201 202 206 public void setIdentifierGeneratorStrategy(String identifierGeneratorStrategy) { 207 this.identifierGeneratorStrategy = identifierGeneratorStrategy; 208 } 209 210 214 public void setNullValue(String nullValue) { 215 this.nullValue = nullValue; 216 } 217 218 public String getForeignKeyName() { 219 return foreignKeyName; 220 } 221 222 public void setForeignKeyName(String foreignKeyName) { 223 this.foreignKeyName = foreignKeyName; 224 } 225 226 public boolean isAlternateUniqueKey() { 227 return alternateUniqueKey; 228 } 229 230 public void setAlternateUniqueKey(boolean unique) { 231 this.alternateUniqueKey = unique; 232 } 233 234 public boolean isNullable() { 235 if ( hasFormula() ) return true; 236 boolean nullable = true; 237 Iterator iter = getColumnIterator(); 238 while ( iter.hasNext() ) { 239 if ( !( (Column) iter.next() ).isNullable() ) { 240 nullable = false; 241 return nullable; } 243 } 244 return nullable; 245 } 246 247 public boolean isSimpleValue() { 248 return true; 249 } 250 251 public boolean isValid(Mapping mapping) throws MappingException { 252 return getColumnSpan()==getType().getColumnSpan(mapping); 253 } 254 255 public Type getType() throws MappingException { 256 if (typeName==null) { 257 throw new MappingException("No type name"); 258 } 259 Type result = TypeFactory.heuristicType(typeName, typeParameters); 260 if (result==null) { 261 String msg = "Could not determine type for: " + typeName; 262 if(columns!=null && columns.size()>0) { 263 msg += ", for columns: " + columns; 264 } 265 throw new MappingException(msg); 266 } 267 return result; 268 } 269 270 public void setTypeUsingReflection(String className, String propertyName) throws MappingException { 271 if (typeName==null) { 272 if (className==null) { 273 throw new MappingException("you must specify types for a dynamic entity: " + propertyName); 274 } 275 typeName = ReflectHelper.reflectedPropertyClass(className, propertyName).getName(); 276 } 277 } 278 279 public boolean isTypeSpecified() { 280 return typeName!=null; 281 } 282 283 public void setTypeParameters(Properties parameterMap) { 284 this.typeParameters = parameterMap; 285 } 286 287 public Properties getTypeParameters() { 288 return typeParameters; 289 } 290 291 public String toString() { 292 return getClass().getName() + '(' + columns.toString() + ')'; 293 } 294 295 public Object accept(ValueVisitor visitor) { 296 return visitor.accept(this); 297 } 298 299 public boolean[] getColumnInsertability() { 300 boolean[] result = new boolean[ getColumnSpan() ]; 301 int i = 0; 302 Iterator iter = getColumnIterator(); 303 while ( iter.hasNext() ) { 304 Selectable s = (Selectable) iter.next(); 305 result[i++] = !s.isFormula(); 306 } 307 return result; 308 } 309 310 public boolean[] getColumnUpdateability() { 311 return getColumnInsertability(); 312 } 313 } 314 | Popular Tags |