Search in sources :

Example 6 with EmbeddedPCMapping

use of org.datanucleus.store.rdbms.mapping.java.EmbeddedPCMapping in project datanucleus-rdbms by datanucleus.

the class ParameterSetter method storeObjectField.

public void storeObjectField(int fieldNumber, Object value) {
    StatementMappingIndex mapIdx = stmtMappings.getMappingForMemberPosition(fieldNumber);
    if (value == null && mapIdx.getMapping().getMemberMetaData().getNullValue() == NullValue.EXCEPTION) {
        throw new NucleusUserException(Localiser.msg("052400", mapIdx.getMapping().getMemberMetaData().getFullFieldName()));
    }
    try {
        JavaTypeMapping mapping = mapIdx.getMapping();
        boolean provideOwner = false;
        if (mapping instanceof EmbeddedPCMapping || mapping instanceof SerialisedPCMapping || mapping instanceof SerialisedReferenceMapping || mapping instanceof PersistableMapping || mapping instanceof InterfaceMapping) {
            // Pass in the owner ObjectProvider/field for any mappings that have relations
            provideOwner = true;
        }
        if (mapIdx.getNumberOfParameterOccurrences() > 0) {
            for (int i = 0; i < mapIdx.getNumberOfParameterOccurrences(); i++) {
                // Set this value for all occurrences of this parameter
                if (provideOwner) {
                    mapping.setObject(ec, statement, mapIdx.getParameterPositionsForOccurrence(i), value, op, fieldNumber);
                } else {
                    mapping.setObject(ec, statement, mapIdx.getParameterPositionsForOccurrence(i), value);
                }
            }
        } else {
            // Important : call setObject even if the paramIndices is null (reachability)
            if (provideOwner) {
                mapping.setObject(ec, statement, null, value, op, fieldNumber);
            } else {
                mapping.setObject(ec, statement, null, value);
            }
        }
        AbstractMemberMetaData mmd = op.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
        RelationType relationType = mmd.getRelationType(ec.getClassLoaderResolver());
        if (op.getClassMetaData().getSCOMutableMemberFlags()[fieldNumber]) {
            SCOUtils.wrapSCOField(op, fieldNumber, value, true);
        } else if (RelationType.isRelationSingleValued(relationType) && (mmd.getEmbeddedMetaData() != null && mmd.getEmbeddedMetaData().getOwnerMember() != null)) {
            // Embedded PC, so make sure the field is wrapped where appropriate TODO This should be part of ManagedRelationships
            op.updateOwnerFieldInEmbeddedField(fieldNumber, value);
        }
    } catch (NotYetFlushedException e) {
        if (op.getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getNullValue() == NullValue.EXCEPTION) {
            throw e;
        }
        op.updateFieldAfterInsert(e.getPersistable(), fieldNumber);
    }
}
Also used : InterfaceMapping(org.datanucleus.store.rdbms.mapping.java.InterfaceMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) SerialisedReferenceMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) EmbeddedPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedPCMapping) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) NotYetFlushedException(org.datanucleus.exceptions.NotYetFlushedException) PersistableMapping(org.datanucleus.store.rdbms.mapping.java.PersistableMapping) RelationType(org.datanucleus.metadata.RelationType) SerialisedPCMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedPCMapping) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 7 with EmbeddedPCMapping

use of org.datanucleus.store.rdbms.mapping.java.EmbeddedPCMapping in project datanucleus-rdbms by datanucleus.

the class ResultSetGetter method fetchObjectField.

public Object fetchObjectField(int fieldNumber) {
    StatementMappingIndex mapIdx = resultMappings.getMappingForMemberPosition(fieldNumber);
    JavaTypeMapping mapping = mapIdx.getMapping();
    AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
    RelationType relationType = mmd.getRelationType(ec.getClassLoaderResolver());
    Object value;
    if (mapping instanceof EmbeddedPCMapping || mapping instanceof SerialisedPCMapping || mapping instanceof SerialisedReferenceMapping) {
        value = mapping.getObject(ec, rs, mapIdx.getColumnPositions(), op, fieldNumber);
    } else {
        if (mmd.isSingleCollection()) {
            StatementClassMapping relationMappings = resultMappings.getMappingDefinitionForMemberPosition(fieldNumber);
            if (relationMappings != null) {
                Class type = ec.getClassLoaderResolver().classForName(mmd.getCollection().getElementType());
                value = processSubObjectFields(mapping, type, relationMappings);
                ElementContainerHandler containerHandler = ec.getTypeManager().getContainerHandler(mmd.getType());
                value = containerHandler.newContainer(mmd, value);
            } else {
                value = mapping.getObject(ec, rs, mapIdx.getColumnPositions());
            }
        } else if (RelationType.isRelationSingleValued(relationType)) {
            // Process fields of sub-object if available in this result set
            StatementClassMapping relationMappings = resultMappings.getMappingDefinitionForMemberPosition(fieldNumber);
            if (relationMappings != null) {
                value = processSubObjectFields(mapping, mmd.getType(), relationMappings);
            } else {
                value = mapping.getObject(ec, rs, mapIdx.getColumnPositions());
            }
        } else {
            value = mapping.getObject(ec, rs, mapIdx.getColumnPositions());
        }
    }
    if (op != null) {
        if (cmd.getSCOMutableMemberFlags()[fieldNumber]) {
            // Wrap any SCO mutable fields
            return SCOUtils.wrapSCOField(op, fieldNumber, value, false);
        } else if (RelationType.isRelationSingleValued(relationType) && (mmd.getEmbeddedMetaData() != null && mmd.getEmbeddedMetaData().getOwnerMember() != null)) {
            // Embedded PC, so make sure the field is wrapped where appropriate TODO This should be part of ManagedRelationships
            op.updateOwnerFieldInEmbeddedField(fieldNumber, value);
            return value;
        }
    }
    return value;
}
Also used : JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) SerialisedReferenceMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping) EmbeddedPCMapping(org.datanucleus.store.rdbms.mapping.java.EmbeddedPCMapping) RelationType(org.datanucleus.metadata.RelationType) SerialisedPCMapping(org.datanucleus.store.rdbms.mapping.java.SerialisedPCMapping) StatementMappingIndex(org.datanucleus.store.rdbms.query.StatementMappingIndex) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) ElementContainerHandler(org.datanucleus.store.types.ElementContainerHandler) StatementClassMapping(org.datanucleus.store.rdbms.query.StatementClassMapping)

Aggregations

AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)7 EmbeddedPCMapping (org.datanucleus.store.rdbms.mapping.java.EmbeddedPCMapping)7 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)7 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)4 RelationType (org.datanucleus.metadata.RelationType)4 PersistableMapping (org.datanucleus.store.rdbms.mapping.java.PersistableMapping)4 ReferenceMapping (org.datanucleus.store.rdbms.mapping.java.ReferenceMapping)4 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 Set (java.util.Set)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)2 ForeignKey (org.datanucleus.store.rdbms.key.ForeignKey)2 PrimaryKey (org.datanucleus.store.rdbms.key.PrimaryKey)2 SerialisedPCMapping (org.datanucleus.store.rdbms.mapping.java.SerialisedPCMapping)2 SerialisedReferenceMapping (org.datanucleus.store.rdbms.mapping.java.SerialisedReferenceMapping)2 StatementMappingIndex (org.datanucleus.store.rdbms.query.StatementMappingIndex)2 ArrayList (java.util.ArrayList)1