1 21 package oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors; 23 24 import java.util.List ; 25 import java.util.ArrayList ; 26 27 import javax.persistence.FetchType; 28 import javax.persistence.CascadeType; 29 import javax.persistence.JoinColumn; 30 import javax.persistence.JoinColumns; 31 import javax.persistence.PrimaryKeyJoinColumn; 32 import javax.persistence.PrimaryKeyJoinColumns; 33 34 import oracle.toplink.essentials.internal.helper.DatabaseField; 35 36 import oracle.toplink.essentials.mappings.DatabaseMapping; 37 import oracle.toplink.essentials.mappings.ForeignReferenceMapping; 38 39 import oracle.toplink.essentials.indirection.ValueHolderInterface; 40 41 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataConstants; 42 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProcessor; 43 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataDescriptor; 44 45 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataJoinColumn; 46 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataJoinColumns; 47 48 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.ClassAccessor; 49 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataAccessibleObject; 50 51 57 public abstract class RelationshipAccessor extends MetadataAccessor { 58 protected String m_mappedBy; 59 protected Class m_targetEntity; 60 protected ArrayList <String > m_cascadeTypes; 61 62 65 protected RelationshipAccessor(MetadataAccessibleObject accessibleObject, ClassAccessor classAccessor) { 66 super(accessibleObject, classAccessor); 67 68 m_mappedBy = ""; 71 setIsOptional(false); 72 m_targetEntity = void.class; 73 setFetchType(MetadataConstants.LAZY); 74 m_cascadeTypes = new ArrayList <String >(); 75 } 76 77 80 public void addCascadeType(String cascadeType) { 81 m_cascadeTypes.add(cascadeType); 82 } 83 84 89 public ArrayList <String > getCascadeTypes() { 90 return m_cascadeTypes; 91 } 92 93 97 protected MetadataJoinColumns getJoinColumns() { 98 JoinColumn joinColumn = getAnnotation(JoinColumn.class); 99 JoinColumns joinColumns = getAnnotation(JoinColumns.class); 100 101 return new MetadataJoinColumns(joinColumns, joinColumn); 102 } 103 104 109 public String getMappedBy() { 110 return m_mappedBy; 111 } 112 113 118 protected DatabaseMapping getOwningMapping() { 119 String ownerAttributeName = m_mappedBy; 120 MetadataDescriptor ownerDescriptor = getReferenceDescriptor(); 121 DatabaseMapping mapping = ownerDescriptor.getMappingForAttributeName(ownerAttributeName); 122 123 if (mapping == null) { 126 m_validator.throwNoMappedByAttributeFound(ownerDescriptor.getJavaClass(), ownerAttributeName, getJavaClass(), getAttributeName()); 127 } 128 129 return mapping; 130 } 131 132 137 public Class getTargetEntity() { 138 return m_targetEntity; 139 } 140 141 145 public boolean hasJoinColumn() { 146 return isAnnotationPresent(JoinColumn.class); 147 } 148 149 153 public boolean hasJoinColumns() { 154 return isAnnotationPresent(JoinColumns.class); 155 } 156 157 162 public boolean hasPrimaryKeyJoinColumns() { 163 return isAnnotationPresent(PrimaryKeyJoinColumns.class) || isAnnotationPresent(PrimaryKeyJoinColumn.class); 164 } 165 166 170 public boolean isOneToOnePrimaryKeyRelationship() { 171 return isOneToOne() && hasPrimaryKeyJoinColumns(); 172 } 173 174 177 protected void processCascadeTypes(ForeignReferenceMapping mapping) { 178 for (String cascadeType : getCascadeTypes()) { 179 setCascadeType(cascadeType, mapping); 180 } 181 182 if (m_descriptor.isCascadePersist() && ! mapping.isCascadePersist()) { 184 setCascadeType(CascadeType.PERSIST.name(), mapping); 185 } 186 } 187 188 193 protected List <MetadataJoinColumn> processJoinColumns() { 194 if (m_descriptor.hasAssociationOverrideFor(getAttributeName())) { 195 return processJoinColumns(m_descriptor.getAssociationOverrideFor(getAttributeName()), getReferenceDescriptor()); 196 } else { 197 return processJoinColumns(getJoinColumns(), getReferenceDescriptor()); 198 } 199 } 200 201 205 protected List <MetadataJoinColumn> processJoinColumns(MetadataJoinColumns joinColumns, MetadataDescriptor descriptor) { 206 if (descriptor.hasCompositePrimaryKey()) { 207 if (joinColumns.size() != descriptor.getPrimaryKeyFields().size()) { 209 m_validator.throwIncompleteJoinColumnsSpecified(getJavaClass(), getAnnotatedElement()); 210 } 211 212 for (MetadataJoinColumn joinColumn : joinColumns.values()) { 214 if (joinColumn.isPrimaryKeyFieldNotSpecified() || joinColumn.isForeignKeyFieldNotSpecified()) { 215 m_validator.throwIncompleteJoinColumnsSpecified(getJavaClass(), getAnnotatedElement()); 216 } 217 } 218 } else { 219 if (joinColumns.size() > 1) { 220 m_validator.throwExcessiveJoinColumnsSpecified(getJavaClass(), getAnnotatedElement()); 221 } 222 } 223 224 return joinColumns.values(); 225 } 226 227 232 public void processRelationship() { 233 if (! isProcessed()) { 236 if (m_descriptor.hasMappingForAttributeName(getAttributeName())) { 237 m_logger.logWarningMessage(m_logger.IGNORE_MAPPING, this); 239 } else { 240 if (hasColumn()) { 242 m_validator.throwRelationshipHasColumnSpecified(getJavaClass(), getAttributeName()); 243 } 244 245 if (getTargetEntity() == ValueHolderInterface.class || (getTargetEntity() == void.class && getReferenceClass() == ValueHolderInterface.class)) { 248 } else { 250 process(); 251 } 252 } 253 254 setIsProcessed(); 256 } 257 } 258 259 263 protected void setCascadeType(String type, ForeignReferenceMapping mapping) { 264 if (type.equals(MetadataConstants.CASCADE_ALL) || type.equals(CascadeType.ALL.name())) { 265 mapping.setCascadeAll(true); 266 } else if (type.equals(MetadataConstants.CASCADE_MERGE) || type.equals(CascadeType.MERGE.name())) { 267 mapping.setCascadeMerge(true); 268 } else if (type.equals(MetadataConstants.CASCADE_PERSIST) || type.equals(CascadeType.PERSIST.name())) { 269 mapping.setCascadePersist(true); 270 } else if (type.equals(MetadataConstants.CASCADE_REFRESH) || type.equals(CascadeType.REFRESH.name())) { 271 mapping.setCascadeRefresh(true); 272 } else if (type.equals(MetadataConstants.CASCADE_REMOVE) || type.equals(CascadeType.REMOVE.name())) { 273 mapping.setCascadeRemove(true); 274 } 275 } 276 277 280 public void setCascadeTypes(CascadeType[] cascadeTypes) { 281 for (CascadeType cascadeType : cascadeTypes) { 282 addCascadeType(cascadeType.name()); 283 } 284 } 285 286 289 public void setMappedBy(String mappedBy) { 290 m_mappedBy = mappedBy; 291 } 292 293 297 public void setReferenceClass(Class potentialReferenceClass, String context) { 298 if (potentialReferenceClass == void.class) { 299 getLogger().logConfigMessage(context, getAnnotatedElement(), getReferenceClass()); 301 } else { 302 m_accessibleObject.setReferenceClass(potentialReferenceClass); 303 } 304 } 305 306 309 public void setTargetEntity(Class targetEntity) { 310 m_targetEntity = targetEntity; 311 } 312 } 313 | Popular Tags |