1 package org.hibernate.cfg; 3 4 import java.util.Map ; 5 import java.util.Set ; 6 import java.util.HashSet ; 7 import java.util.Iterator ; 8 import javax.persistence.PrimaryKeyJoinColumn; 9 import javax.persistence.JoinColumn; 10 11 import org.hibernate.AnnotationException; 12 import org.hibernate.util.StringHelper; 13 import org.hibernate.mapping.Column; 14 import org.hibernate.mapping.Join; 15 import org.hibernate.mapping.PersistentClass; 16 import org.hibernate.mapping.SimpleValue; 17 import org.hibernate.mapping.Value; 18 19 25 public class Ejb3JoinColumn extends Ejb3Column { 26 private String propertyName; 27 28 private String referencedColumn; 29 private String mappedBy; 30 private String defaultColumnHeader; 31 32 public void setPropertyName(String propertyName) { 33 this.propertyName = propertyName; 34 } 35 36 public void setReferencedColumn(String referencedColumn) { 37 this.referencedColumn = referencedColumn; 38 } 39 40 public void setDefaultColumnHeader(String defaultColumnHeader) { 41 this.defaultColumnHeader = defaultColumnHeader; 42 } 43 44 public String getPropertyName() { 45 return propertyName; 46 } 47 48 public String getMappedBy() { 49 return mappedBy; 50 } 51 52 public void setMappedBy(String mappedBy) { 53 this.mappedBy = mappedBy; 54 } 55 56 public Ejb3JoinColumn() { 57 setMappedBy(AnnotationBinder.ANNOTATION_STRING_DEFAULT); 58 } 59 60 public Ejb3JoinColumn(String sqlType, 61 String name, 62 boolean nullable, 63 boolean unique, 64 boolean insertable, 65 boolean updatable, 66 String referencedColumn, 67 String secondaryTable, 68 Map <String , Join> joins, 69 PropertyHolder propertyHolder, 70 String propertyName, 71 String mappedBy, 72 boolean isImplicit, 73 Mappings mappings) { 74 super(); 75 setImplicit(isImplicit); 76 setSqlType(sqlType); 77 setColumnName(name); 78 setNullable( nullable ); 79 setUnique( unique ); 80 setInsertable( insertable ); 81 setUpdatable( updatable ); 82 setSecondaryTableName( secondaryTable ); 83 setPropertyHolder(propertyHolder); 84 setJoins(joins); 85 setMappings(mappings); 86 bind(); 87 this.referencedColumn = referencedColumn; 101 this.propertyName = propertyName; 102 this.mappedBy = mappedBy; 103 } 104 105 public String getReferencedColumn() { 106 return referencedColumn; 107 } 108 109 public static Ejb3JoinColumn buildJoinColumn( 110 JoinColumn ann, 111 Map <String , Join> joins, 112 PropertyHolder propertyHolder, 113 String propertyName, 114 ExtendedMappings mappings 115 ) { 116 if (ann != null) { 117 Ejb3JoinColumn joinColumn = new Ejb3JoinColumn(); 118 joinColumn.setJoinAnnotation(ann, null); 119 joinColumn.setJoins(joins); 120 joinColumn.setPropertyHolder(propertyHolder); 121 joinColumn.setPropertyName(propertyName); 122 joinColumn.setImplicit(false); 123 joinColumn.setMappings(mappings); 124 joinColumn.bind(); 125 return joinColumn; 126 } 127 else { 128 Ejb3JoinColumn joinColumn = new Ejb3JoinColumn(); 129 joinColumn.setJoins(joins); 130 joinColumn.setPropertyHolder(propertyHolder); 131 joinColumn.setPropertyName(propertyName); 132 joinColumn.setImplicit(true); 133 joinColumn.setMappings(mappings); 134 joinColumn.bind(); 135 return joinColumn; 136 } 138 } 139 140 141 public void setJoinAnnotation(JoinColumn annJoin, String defaultName) { 143 if (annJoin == null) { 144 setImplicit(true); 145 } 146 else { 147 setImplicit(false); 148 if ( ! AnnotationBinder.isDefault( annJoin.columnDefinition() ) ) setSqlType( annJoin.columnDefinition() ); 149 if ( ! AnnotationBinder.isDefault( annJoin.name() ) ) setColumnName( annJoin.name() ); 150 setNullable( annJoin.nullable() ); 151 setUnique( annJoin.unique() ); 152 setInsertable( annJoin.insertable() ); 153 setUpdatable( annJoin.updatable() ); 154 setReferencedColumn( annJoin.referencedColumnName() ); 155 setSecondaryTableName( annJoin.secondaryTable() ); 156 } 157 } 158 159 public static Ejb3JoinColumn buildImplicitJoinColumn(String mappedBy, 160 Map <String , Join> joins, 161 PropertyHolder propertyHolder, 162 String propertyName, 163 ExtendedMappings mappings) { 164 return new Ejb3JoinColumn( (String ) null, null, 165 true, false, true, true, null, (String ) null, joins, 166 propertyHolder, propertyName, mappedBy, true, mappings ); 167 } 168 169 180 public static Ejb3JoinColumn buildJoinColumn(JoinColumn ann, 181 Value identifier, 182 Map <String , Join> joins, 183 PropertyHolder propertyHolder, ExtendedMappings mappings) { 184 185 Column col = (Column) identifier.getColumnIterator().next(); 186 String defaultName = col.getName(); 187 if (ann != null) { 188 String sqlType = ann.columnDefinition().equals("") ? null : ann.columnDefinition(); 189 String name = ann.name().equals("") ? defaultName : ann.name(); 190 return new Ejb3JoinColumn(sqlType, 191 name, ann.nullable(), ann.unique(), 192 ann.insertable(), ann.updatable(), 193 ann.referencedColumnName(), 194 ann.secondaryTable(), 195 joins, 196 propertyHolder, null, null, false, mappings); 197 } 198 else { 199 return new Ejb3JoinColumn( (String ) null, defaultName, 200 true, false, true, true, null, (String ) null, 201 joins, propertyHolder, null, null, false, mappings 202 ); 203 } 204 } 205 206 public static Ejb3JoinColumn buildJoinColumn(PrimaryKeyJoinColumn ann, 207 Value identifier, 208 Map <String , Join> joins, 209 PropertyHolder propertyHolder, ExtendedMappings mappings) { 210 211 Column col = (Column) identifier.getColumnIterator().next(); 212 String defaultName = col.getName(); 213 if (ann != null) { 214 String sqlType = ann.columnDefinition().equals("") ? null : ann.columnDefinition(); 215 String name = ann.name().equals("") ? defaultName : ann.name(); 216 return new Ejb3JoinColumn(sqlType, 217 name, false, false, 218 true, true, 219 ann.referencedColumnName(), 220 null, joins, 221 propertyHolder, null, null, false, mappings); 222 } 223 else { 224 return new Ejb3JoinColumn( (String ) null, defaultName, 225 false, false, true, true, null, (String ) null, 226 joins, propertyHolder, null, null, true, mappings 227 ); 228 } 229 } 230 233 public void setPersistentClass(PersistentClass persistentClass) { 234 this.propertyHolder = PropertyHolderBuilder.buildPropertyHolder(persistentClass); 235 } 236 237 public static void checkIfJoinColumn(Object columns, PropertyHolder holder, PropertyInferredData property) { 238 if ( ! ( columns instanceof Ejb3JoinColumn[] ) ) { 239 throw new AnnotationException( 240 "@Column cannot be used on an association property: " 241 + holder.getEntityName() 242 + "." 243 + property.getPropertyName() 244 ); 245 } 246 } 247 248 public void linkValueUsingDefaultColumnNaming(Column column, SimpleValue value) { 249 String columnName; 250 final boolean isAnAssociation = defaultColumnHeader != null || propertyName != null; 251 if (isAnAssociation) { 252 columnName = (defaultColumnHeader == null ? propertyName : defaultColumnHeader) + "_" + column.getName(); 253 } 254 else { 255 columnName = column.getName(); 257 } 258 initMappingColumn( 259 columnName, 260 column.getLength(), 261 column.getPrecision(), 262 column.getScale(), 263 getMappingColumn().isNullable(), 264 column.getSqlType(), 265 getMappingColumn().isUnique() ); 266 linkWithValue(value); 267 } 268 269 public void linkValueUsingAColumnCopy(Column column, SimpleValue value) { 270 initMappingColumn( 271 column.getName(), 272 column.getLength(), 273 column.getPrecision(), 274 column.getScale(), 275 getMappingColumn().isNullable(), 276 column.getSqlType(), 277 getMappingColumn().isUnique() ); 278 linkWithValue(value); 279 } 280 281 public static final int NO_REFERENCE = 0; 284 public static final int PK_REFERENCE = 1; 286 public static final int NON_PK_REFERENCE = 2; 288 289 public static int checkReferencedColumnsType(Ejb3JoinColumn[] columns, PersistentClass referencedEntity) { 290 Set <Column> idColumns = new HashSet <Column>(); 292 Iterator idColumnsIt = referencedEntity.getIdentifier().getColumnIterator(); 293 while ( idColumnsIt.hasNext() ) { 294 idColumns.add( (Column) idColumnsIt.next() ); 295 } 296 297 boolean isFkReferencedColumnName = false; 298 boolean noReferencedColumn = true; 299 300 for (Ejb3JoinColumn ejb3Column : columns) { 302 String referencedColumnName = ejb3Column.getReferencedColumn(); 303 if (! StringHelper.isEmpty( referencedColumnName ) ) { 304 noReferencedColumn = false; 305 Column refCol = new Column(referencedColumnName); 306 boolean contains = idColumns.contains( refCol ); 307 if ( ! contains ) { 308 isFkReferencedColumnName = true; 309 } 310 } 311 } 312 if (isFkReferencedColumnName) { 313 return NON_PK_REFERENCE; 314 } 315 else if (noReferencedColumn) { 316 return NO_REFERENCE; 317 } 318 else { 319 return PK_REFERENCE; 320 } 321 } 322 323 } | Popular Tags |