use of org.datanucleus.metadata.AbstractMemberMetaData in project datanucleus-rdbms by datanucleus.
the class ClassTable method getMetaDataForExternalMapping.
/**
* Accessor for the MetaData for the (owner) field that an external mapping corresponds to.
* @param mapping The mapping
* @param mappingType The mapping type
* @return metadata for the external mapping
*/
public AbstractMemberMetaData getMetaDataForExternalMapping(JavaTypeMapping mapping, MappingType mappingType) {
if (mappingType == MappingType.EXTERNAL_FK) {
Set entries = getExternalFkMappings().entrySet();
Iterator iter = entries.iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
if (entry.getValue() == mapping) {
return (AbstractMemberMetaData) entry.getKey();
}
}
} else if (mappingType == MappingType.EXTERNAL_FK_DISCRIMINATOR) {
Set entries = getExternalFkDiscriminatorMappings().entrySet();
Iterator iter = entries.iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
if (entry.getValue() == mapping) {
return (AbstractMemberMetaData) entry.getKey();
}
}
} else if (mappingType == MappingType.EXTERNAL_INDEX) {
Set entries = getExternalOrderMappings().entrySet();
Iterator iter = entries.iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
if (entry.getValue() == mapping) {
return (AbstractMemberMetaData) entry.getKey();
}
}
}
return null;
}
use of org.datanucleus.metadata.AbstractMemberMetaData in project datanucleus-rdbms by datanucleus.
the class ClassTable method getMetaDataForMember.
/**
* Acessor for the FieldMetaData for the field with the specified name.
* Searches the MetaData of all classes managed by this table.
* Doesn't allow for cases where the table manages subclasses with the same field name.
* In that case you should use the equivalent method passing FieldMetaData.
* TODO Support subclasses with fields of the same name
* TODO Use of this is discouraged since the fieldName is not fully qualified
* and if a superclass-table inheritance is used we could have 2 fields of that name here.
* @param memberName the field/property name
* @return metadata for the member
*/
AbstractMemberMetaData getMetaDataForMember(String memberName) {
// TODO Dont search "cmd" since it is included in "managedClassMetaData"
AbstractMemberMetaData mmd = cmd.getMetaDataForMember(memberName);
if (mmd == null) {
Iterator<AbstractClassMetaData> iter = managedClassMetaData.iterator();
while (iter.hasNext()) {
AbstractClassMetaData theCmd = iter.next();
final AbstractMemberMetaData foundMmd = theCmd.getMetaDataForMember(memberName);
if (foundMmd != null) {
if (mmd != null && (!mmd.toString().equalsIgnoreCase(foundMmd.toString()) || mmd.getType() != foundMmd.getType())) {
final String errMsg = "Table " + getIdentifier() + " manages at least 2 subclasses that both define a field \"" + memberName + "\", " + "and the fields' metadata is different or they have different type! That means you can get e.g. wrong fetch results.";
NucleusLogger.DATASTORE_SCHEMA.error(errMsg);
throw new NucleusException(errMsg).setFatal();
}
mmd = foundMmd;
}
}
}
return mmd;
}
use of org.datanucleus.metadata.AbstractMemberMetaData in project datanucleus-rdbms by datanucleus.
the class ClassTable method provideExternalMappings.
/**
* Provide the mappings to the consumer for all external fields mapped to this table
* of the specified type
* @param consumer Consumer for the mappings
* @param mappingType Type of external mapping
*/
public final void provideExternalMappings(MappingConsumer consumer, MappingType mappingType) {
if (mappingType == MappingType.EXTERNAL_FK && externalFkMappings != null) {
consumer.preConsumeMapping(highestMemberNumber + 1);
Iterator<AbstractMemberMetaData> iter = externalFkMappings.keySet().iterator();
while (iter.hasNext()) {
AbstractMemberMetaData fmd = iter.next();
JavaTypeMapping fieldMapping = externalFkMappings.get(fmd);
if (fieldMapping != null) {
consumer.consumeMapping(fieldMapping, MappingType.EXTERNAL_FK);
}
}
} else if (mappingType == MappingType.EXTERNAL_FK_DISCRIMINATOR && externalFkDiscriminatorMappings != null) {
consumer.preConsumeMapping(highestMemberNumber + 1);
Iterator<AbstractMemberMetaData> iter = externalFkDiscriminatorMappings.keySet().iterator();
while (iter.hasNext()) {
AbstractMemberMetaData fmd = iter.next();
JavaTypeMapping fieldMapping = externalFkDiscriminatorMappings.get(fmd);
if (fieldMapping != null) {
consumer.consumeMapping(fieldMapping, MappingType.EXTERNAL_FK_DISCRIMINATOR);
}
}
} else if (mappingType == MappingType.EXTERNAL_INDEX && externalOrderMappings != null) {
consumer.preConsumeMapping(highestMemberNumber + 1);
Iterator<AbstractMemberMetaData> iter = externalOrderMappings.keySet().iterator();
while (iter.hasNext()) {
AbstractMemberMetaData fmd = iter.next();
JavaTypeMapping fieldMapping = externalOrderMappings.get(fmd);
if (fieldMapping != null) {
consumer.consumeMapping(fieldMapping, MappingType.EXTERNAL_INDEX);
}
}
}
}
use of org.datanucleus.metadata.AbstractMemberMetaData in project datanucleus-rdbms by datanucleus.
the class ClassTable method getMemberMapping.
/**
* Accessor for the field mapping for the named field.
* The field may exist in a parent table or a secondary table.
* Throws a NoSuchPersistentFieldException if the field name is not found.
* TODO Use of this is discouraged since the fieldName is not fully qualified
* and if a superclass-table inheritance is used we could have 2 fields of that name here.
* @param memberName Name of field/property
* @return The mapping.
* @throws NoSuchPersistentFieldException Thrown when the field/property is not found
*/
public JavaTypeMapping getMemberMapping(String memberName) {
assertIsInitialized();
AbstractMemberMetaData mmd = getMetaDataForMember(memberName);
JavaTypeMapping m = getMemberMapping(mmd);
if (m == null) {
throw new NoSuchPersistentFieldException(cmd.getFullClassName(), memberName);
}
return m;
}
use of org.datanucleus.metadata.AbstractMemberMetaData in project datanucleus-rdbms by datanucleus.
the class ClassTable method getExpectedCandidateKeys.
/**
* Accessor for the expected candidate keys for this table.
* @return The expected candidate keys.
*/
protected List<CandidateKey> getExpectedCandidateKeys() {
assertIsInitialized();
// The candidate keys required by the basic table
List<CandidateKey> candidateKeys = super.getExpectedCandidateKeys();
// Add any constraints required for a FK map
Iterator<CandidateKey> cks = candidateKeysByMapField.values().iterator();
while (cks.hasNext()) {
CandidateKey ck = cks.next();
candidateKeys.add(ck);
}
// Add on any user-required candidate keys for the fields
Set fieldNumbersSet = memberMappingsMap.keySet();
Iterator iter = fieldNumbersSet.iterator();
while (iter.hasNext()) {
AbstractMemberMetaData fmd = (AbstractMemberMetaData) iter.next();
JavaTypeMapping fieldMapping = memberMappingsMap.get(fmd);
if (fieldMapping instanceof EmbeddedPCMapping) {
// Add indexes for fields of this embedded PC object
EmbeddedPCMapping embMapping = (EmbeddedPCMapping) fieldMapping;
for (int i = 0; i < embMapping.getNumberOfJavaTypeMappings(); i++) {
JavaTypeMapping embFieldMapping = embMapping.getJavaTypeMapping(i);
UniqueMetaData umd = embFieldMapping.getMemberMetaData().getUniqueMetaData();
if (umd != null) {
CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, embFieldMapping);
if (ck != null) {
candidateKeys.add(ck);
}
}
}
} else {
// Add any required candidate key for this field
UniqueMetaData umd = fmd.getUniqueMetaData();
if (umd != null) {
CandidateKey ck = TableUtils.getCandidateKeyForField(this, umd, fieldMapping);
if (ck != null) {
candidateKeys.add(ck);
}
}
}
}
// Add on any user-required candidate keys for the class(es) as a whole (composite keys)
Iterator<AbstractClassMetaData> cmdIter = managedClassMetaData.iterator();
while (cmdIter.hasNext()) {
AbstractClassMetaData thisCmd = cmdIter.next();
List<UniqueMetaData> classCKs = thisCmd.getUniqueMetaData();
if (classCKs != null) {
for (UniqueMetaData unimd : classCKs) {
CandidateKey ck = getCandidateKeyForUniqueMetaData(unimd);
if (ck != null) {
candidateKeys.add(ck);
}
}
}
}
if (cmd.getIdentityType() == IdentityType.APPLICATION) {
// Make sure there is no reuse of PK fields that cause a duplicate index for the PK. Remove it if required
PrimaryKey pk = getPrimaryKey();
Iterator<CandidateKey> candidatesIter = candidateKeys.iterator();
while (candidatesIter.hasNext()) {
CandidateKey key = candidatesIter.next();
if (key.getColumnList().equals(pk.getColumnList())) {
NucleusLogger.DATASTORE_SCHEMA.debug("Candidate key " + key + " is for the same columns as the PrimaryKey so being removed from expected set of candidates. PK is always unique");
candidatesIter.remove();
}
}
}
return candidateKeys;
}
Aggregations