Search in sources :

Example 1 with NullValueException

use of org.datanucleus.store.rdbms.exceptions.NullValueException in project datanucleus-rdbms by datanucleus.

the class MultiPersistableMapping method getObject.

/**
 * Method to retrieve an object of this type from the ResultSet.
 * @param ec execution context
 * @param rs The ResultSet
 * @param pos The parameter positions
 * @return The object
 */
public Object getObject(ExecutionContext ec, final ResultSet rs, int[] pos) {
    // Go through the possible types for this field and find a non-null value (if there is one)
    int n = 0;
    for (int i = 0; i < javaTypeMappings.length; i++) {
        int[] posMapping;
        if (n >= pos.length) {
            // this means we store all implementations to the same columns, so we reset the index
            n = 0;
        }
        if (javaTypeMappings[i].getReferenceMapping() != null) {
            posMapping = new int[javaTypeMappings[i].getReferenceMapping().getNumberOfDatastoreMappings()];
        } else {
            posMapping = new int[javaTypeMappings[i].getNumberOfDatastoreMappings()];
        }
        for (int j = 0; j < posMapping.length; j++) {
            posMapping[j] = pos[n++];
        }
        Object value = null;
        try {
            // Retrieve the value (PC object) for this mappings' object
            value = javaTypeMappings[i].getObject(ec, rs, posMapping);
            if (value != null) {
                if (IdentityUtils.isDatastoreIdentity(value)) {
                    // What situation is this catering for exactly ?
                    Column col = null;
                    if (javaTypeMappings[i].getReferenceMapping() != null) {
                        col = javaTypeMappings[i].getReferenceMapping().getDatastoreMapping(0).getColumn();
                    } else {
                        col = javaTypeMappings[i].getDatastoreMapping(0).getColumn();
                    }
                    String className = col.getStoredJavaType();
                    value = ec.getNucleusContext().getIdentityManager().getDatastoreId(className, IdentityUtils.getTargetKeyForDatastoreIdentity(value));
                    return ec.findObject(value, false, true, null);
                } else if (ec.getClassLoaderResolver().classForName(getType()).isAssignableFrom(value.getClass())) {
                    return value;
                }
            }
        } catch (NullValueException e) {
        // expected if implementation object is null and has primitive fields in the primary key
        } catch (NucleusObjectNotFoundException onfe) {
        // expected, will try next implementation
        }
    }
    return null;
}
Also used : Column(org.datanucleus.store.rdbms.table.Column) NucleusObjectNotFoundException(org.datanucleus.exceptions.NucleusObjectNotFoundException) NullValueException(org.datanucleus.store.rdbms.exceptions.NullValueException)

Example 2 with NullValueException

use of org.datanucleus.store.rdbms.exceptions.NullValueException in project datanucleus-rdbms by datanucleus.

the class RealRDBMSMapping method getObject.

public Object getObject(ResultSet rs, int param) {
    Object value;
    try {
        float f = rs.getFloat(param);
        value = rs.wasNull() ? null : f;
    } catch (SQLException e) {
        try {
            // when value is real in database, cause cause a parse error when calling getFloat
            // JDBC error:Value can not be converted to requested type.
            value = new Float(Float.parseFloat(rs.getString(param)));
            if (column == null || column.getColumnMetaData() == null || !column.getColumnMetaData().isAllowsNull()) {
                if (rs.wasNull()) {
                    throw new NullValueException(Localiser.msg("055003", column));
                }
            }
        } catch (SQLException e1) {
            try {
                throw new NucleusDataStoreException("Can't get float result: param = " + param + " - " + rs.getString(param), e);
            } catch (SQLException e2) {
                throw new NucleusDataStoreException(Localiser.msg("055002", "Object", "" + param, column, e.getMessage()), e);
            }
        }
    }
    return value;
}
Also used : NucleusDataStoreException(org.datanucleus.exceptions.NucleusDataStoreException) SQLException(java.sql.SQLException) NullValueException(org.datanucleus.store.rdbms.exceptions.NullValueException)

Aggregations

NullValueException (org.datanucleus.store.rdbms.exceptions.NullValueException)2 SQLException (java.sql.SQLException)1 NucleusDataStoreException (org.datanucleus.exceptions.NucleusDataStoreException)1 NucleusObjectNotFoundException (org.datanucleus.exceptions.NucleusObjectNotFoundException)1 Column (org.datanucleus.store.rdbms.table.Column)1