1 3 package net.sf.persist; 4 5 import java.lang.reflect.Method ; 6 import java.util.HashMap ; 7 import java.util.Map ; 8 import java.util.Set ; 9 10 18 public class NoTableMapping extends Mapping { 19 20 private final Class objectClass; 22 23 private final Map <String , Method > settersMap; 25 26 private final Map <String , Method > gettersMap; 28 29 private final Map <String , String > columnsMap; 31 32 public NoTableMapping(Class objectClass, NameGuesser nameGuesser) { 33 34 checkAnnotation(objectClass); 35 36 this.objectClass = objectClass; 37 38 Map [] fieldsMaps = Mapping.getFieldsMaps(objectClass); 40 final Map <String , net.sf.persist.annotations.Column> annotationsMap = fieldsMaps[0]; 41 gettersMap = fieldsMaps[1]; 42 settersMap = fieldsMaps[2]; 43 44 columnsMap = new HashMap (); 49 for (String fieldName : gettersMap.keySet()) { 50 51 net.sf.persist.annotations.Column annotation = annotationsMap.get(fieldName); 52 53 if (annotation != null) { 55 if (annotation.autoGenerated() == true) { 56 throw new RuntimeSQLException("@Column(autoGenerated=true) is set for field [" + fieldName 57 + "] of class [" + objectClass.getCanonicalName() 58 + " which has been declared with @NoTable"); 59 } 60 } 61 62 if (annotation != null && annotation.name() != null) { 64 65 if (annotation.name().trim().equals("")) { 67 throw new RuntimeSQLException("@Column annotation for field [" + fieldName + "] of class [" 68 + objectClass.getCanonicalName() + "] defines a blank column name"); 69 } 70 71 checkNameConflicts(fieldName, annotation.name()); 73 74 columnsMap.put(annotation.name(), fieldName); 76 } 77 78 else { 80 Set <String > guessedColumns = nameGuesser.guessColumn(fieldName); 81 for (String guessedColumn : guessedColumns) { 82 83 checkNameConflicts(fieldName, guessedColumn); 85 86 columnsMap.put(guessedColumn, fieldName); 88 } 89 } 90 } 91 92 } 93 94 98 public String getFieldNameForColumn(String columnName) { 99 String fieldName = columnsMap.get(columnName); 100 if (fieldName == null) { 101 throw new RuntimeSQLException("Could map field for column [" + columnName + "] on class [" 102 + objectClass.getCanonicalName() 103 + "]. Please specify an explict @Column annotation for that column."); 104 } 105 return fieldName; 106 } 107 108 114 public Method getSetterForColumn(String columnName) { 115 String fieldName = getFieldNameForColumn(columnName); 116 return settersMap.get(fieldName); 117 } 118 119 125 public Method getGetterForColumn(String columnName) { 126 String fieldName = getFieldNameForColumn(columnName); 127 return gettersMap.get(fieldName); 128 } 129 130 134 private void checkNameConflicts(String fieldName, String column) { 135 String existingFieldName = columnsMap.get(column); 136 if (existingFieldName != null) { 137 throw new RuntimeSQLException("Fields [" + fieldName + "] and [" + existingFieldName 138 + "] have conflicting column name [" + column 139 + "] either from guessed names or anotations. " 140 + "Please specify @Column mappings for at least one of those fields"); 141 } 142 } 143 144 149 private void checkAnnotation(Class objectClass) { 150 final net.sf.persist.annotations.NoTable noTableAnnotation = (net.sf.persist.annotations.NoTable) objectClass 152 .getAnnotation(net.sf.persist.annotations.NoTable.class); 153 154 if (noTableAnnotation == null) { 156 throw new RuntimeSQLException("Class [" + objectClass.getCanonicalName() 157 + "] does not specify a @NoTable annotation therefore it can't be mapped through NoTableMapping"); 158 } 159 160 final net.sf.persist.annotations.Table tableAnnotation = (net.sf.persist.annotations.Table) objectClass 162 .getAnnotation(net.sf.persist.annotations.Table.class); 163 164 if (tableAnnotation != null) { 165 throw new RuntimeSQLException("Class [" + objectClass.getCanonicalName() 166 + "] specifies conflicting @Table and @NoTable annotations"); 167 } 168 } 169 170 } 171 | Popular Tags |