Search in sources :

Example 1 with ObjectJdbcType

use of org.hibernate.type.descriptor.jdbc.ObjectJdbcType in project hibernate-orm by hibernate.

the class InferredBasicValueResolver method from.

public static <T> BasicValue.Resolution<T> from(BasicJavaType<T> explicitJavaType, JdbcType explicitJdbcType, Type resolvedJavaType, Supplier<JavaType<T>> reflectedJtdResolver, JdbcTypeIndicators stdIndicators, Table table, Selectable selectable, String ownerName, String propertyName, TypeConfiguration typeConfiguration) {
    final JavaType<T> reflectedJtd = reflectedJtdResolver.get();
    // NOTE : the distinction that is made below wrt `explicitJavaType` and `reflectedJtd` is
    // needed temporarily to trigger "legacy resolution" versus "ORM6 resolution.  Yes, it
    // makes the code a little more complicated but the benefit is well worth it - saving memory
    final BasicType<T> jdbcMapping;
    final BasicType<T> legacyType;
    if (explicitJavaType != null) {
        if (explicitJdbcType != null) {
            // we also have an explicit JdbcType
            jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(explicitJavaType, explicitJdbcType);
            legacyType = jdbcMapping;
        } else {
            if (explicitJavaType instanceof TemporalJavaType) {
                return fromTemporal((TemporalJavaType<T>) explicitJavaType, null, null, resolvedJavaType, stdIndicators, typeConfiguration);
            }
            // we need to infer the JdbcType and use that to build the value-mapping
            final JdbcType inferredJdbcType = explicitJavaType.getRecommendedJdbcType(stdIndicators);
            if (inferredJdbcType instanceof ObjectJdbcType) {
                if (explicitJavaType instanceof EnumJavaType) {
                    return fromEnum((EnumJavaType) reflectedJtd, explicitJavaType, null, stdIndicators, typeConfiguration);
                } else if (explicitJavaType instanceof TemporalJavaType) {
                    return fromTemporal((TemporalJavaType<T>) reflectedJtd, explicitJavaType, null, resolvedJavaType, stdIndicators, typeConfiguration);
                } else if (explicitJavaType instanceof SerializableJavaType || explicitJavaType.getJavaType() instanceof Serializable) {
                    legacyType = new SerializableType(explicitJavaType);
                    jdbcMapping = legacyType;
                } else {
                    jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(explicitJavaType, inferredJdbcType);
                    legacyType = jdbcMapping;
                }
            } else {
                jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(explicitJavaType, inferredJdbcType);
                legacyType = jdbcMapping;
            }
        }
    } else if (reflectedJtd != null) {
        // we were able to determine the "reflected java-type"
        if (explicitJdbcType != null) {
            // we also have an explicit JdbcType
            jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(reflectedJtd, explicitJdbcType);
            legacyType = jdbcMapping;
        } else {
            if (reflectedJtd instanceof EnumJavaType) {
                return fromEnum((EnumJavaType) reflectedJtd, null, null, stdIndicators, typeConfiguration);
            } else if (reflectedJtd instanceof TemporalJavaType) {
                return fromTemporal((TemporalJavaType<T>) reflectedJtd, null, null, resolvedJavaType, stdIndicators, typeConfiguration);
            } else {
                // see if there is a registered BasicType for this JavaType and, if so, use it.
                // this mimics the legacy handling
                final BasicType<T> registeredType = typeConfiguration.getBasicTypeRegistry().getRegisteredType(reflectedJtd.getJavaType());
                if (registeredType != null) {
                    // so here is the legacy resolution
                    legacyType = resolveSqlTypeIndicators(stdIndicators, registeredType, reflectedJtd);
                    jdbcMapping = legacyType;
                } else {
                    // there was not a "legacy" BasicType registration,  so use `JavaType#getRecommendedJdbcType`, if
                    // one, to create a mapping
                    final JdbcType recommendedJdbcType = reflectedJtd.getRecommendedJdbcType(stdIndicators);
                    if (recommendedJdbcType != null) {
                        jdbcMapping = typeConfiguration.getBasicTypeRegistry().resolve(reflectedJtd, recommendedJdbcType);
                        legacyType = jdbcMapping;
                    } else if (reflectedJtd instanceof SerializableJavaType || Serializable.class.isAssignableFrom(reflectedJtd.getJavaTypeClass())) {
                        legacyType = new SerializableType(reflectedJtd);
                        jdbcMapping = legacyType;
                    } else {
                        // let this fall through to the exception creation below
                        legacyType = null;
                        jdbcMapping = null;
                    }
                }
            }
        }
    } else {
        if (explicitJdbcType != null) {
            // we have an explicit STD, but no JTD - infer JTD
            // - NOTE : yes its an odd case, but its easy to implement here, so...
            Integer length = null;
            Integer scale = null;
            if (selectable instanceof Column) {
                final Column column = (Column) selectable;
                if (column.getPrecision() != null && column.getPrecision() > 0) {
                    length = column.getPrecision();
                    scale = column.getScale();
                } else if (column.getLength() != null) {
                    if (column.getLength() > (long) Integer.MAX_VALUE) {
                        length = Integer.MAX_VALUE;
                    } else {
                        length = column.getLength().intValue();
                    }
                }
            }
            final BasicJavaType<T> recommendedJtd = explicitJdbcType.getJdbcRecommendedJavaTypeMapping(length, scale, typeConfiguration);
            jdbcMapping = resolveSqlTypeIndicators(stdIndicators, typeConfiguration.getBasicTypeRegistry().resolve(recommendedJtd, explicitJdbcType), recommendedJtd);
            legacyType = jdbcMapping;
        } else {
            throw new MappingException("Could not determine JavaType nor JdbcType to use" + " for BasicValue: owner = " + ownerName + "; property = " + propertyName + "; table = " + table.getName() + "; column = " + selectable.getText());
        }
    }
    if (jdbcMapping == null) {
        throw new MappingException("Could not determine JavaType nor JdbcType to use" + "" + " for " + ((BasicValue) stdIndicators).getResolvedJavaType() + "; table = " + table.getName() + "; column = " + selectable.getText());
    }
    return new InferredBasicValueResolution<>(jdbcMapping, jdbcMapping.getJavaTypeDescriptor(), jdbcMapping.getJavaTypeDescriptor(), jdbcMapping.getJdbcType(), null, legacyType, null);
}
Also used : Serializable(java.io.Serializable) SerializableJavaType(org.hibernate.type.descriptor.java.SerializableJavaType) SerializableType(org.hibernate.type.SerializableType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) ObjectJdbcType(org.hibernate.type.descriptor.jdbc.ObjectJdbcType) MappingException(org.hibernate.MappingException) BasicValue(org.hibernate.mapping.BasicValue) TemporalJavaType(org.hibernate.type.descriptor.java.TemporalJavaType) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) ObjectJdbcType(org.hibernate.type.descriptor.jdbc.ObjectJdbcType) EnumJavaType(org.hibernate.type.descriptor.java.EnumJavaType) Column(org.hibernate.mapping.Column)

Example 2 with ObjectJdbcType

use of org.hibernate.type.descriptor.jdbc.ObjectJdbcType in project hibernate-orm by hibernate.

the class BasicTypeRegistryTest method testOverriding.

@Test
public void testOverriding() {
    TypeConfiguration typeConfiguration = new TypeConfiguration();
    BasicTypeRegistry registry = typeConfiguration.getBasicTypeRegistry();
    BasicType<?> uuidBinaryRegistration = registry.getRegisteredType("uuid-binary");
    assertTrue(uuidBinaryRegistration.getJavaTypeDescriptor() instanceof UUIDJavaType);
    assertTrue(uuidBinaryRegistration.getJdbcType() instanceof BinaryJdbcType);
    final BasicType<UUID> uuidRegistration = registry.getRegisteredType(UUID.class.getName());
    assertTrue(uuidRegistration.getJavaTypeDescriptor() instanceof UUIDJavaType);
    assertTrue(uuidRegistration.getJdbcType() instanceof ObjectJdbcType);
    final BasicType<?> override = new BasicTypeImpl<>(UUIDJavaType.INSTANCE, CharJdbcType.INSTANCE);
    registry.register(override, UUID.class.getName());
    final BasicType<Object> overrideRegistration = registry.getRegisteredType(UUID.class.getName());
    assertSame(override, overrideRegistration);
    assertNotSame(uuidBinaryRegistration, overrideRegistration);
    assertNotSame(uuidRegistration, overrideRegistration);
}
Also used : ObjectJdbcType(org.hibernate.type.descriptor.jdbc.ObjectJdbcType) BinaryJdbcType(org.hibernate.type.descriptor.jdbc.BinaryJdbcType) BasicTypeImpl(org.hibernate.type.internal.BasicTypeImpl) BasicTypeRegistry(org.hibernate.type.BasicTypeRegistry) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) UUID(java.util.UUID) UUIDJavaType(org.hibernate.type.descriptor.java.UUIDJavaType) Test(org.junit.Test)

Example 3 with ObjectJdbcType

use of org.hibernate.type.descriptor.jdbc.ObjectJdbcType in project hibernate-orm by hibernate.

the class JdbcTypeRegistry method getDescriptor.

public JdbcType getDescriptor(int jdbcTypeCode) {
    JdbcType descriptor = descriptorMap.get(jdbcTypeCode);
    if (descriptor != null) {
        return descriptor;
    }
    if (JdbcTypeNameMapper.isStandardTypeCode(jdbcTypeCode)) {
        log.debugf("A standard JDBC type code [%s] was not defined in SqlTypeDescriptorRegistry", jdbcTypeCode);
    }
    // see if the typecode is part of a known type family...
    JdbcTypeFamilyInformation.Family family = JdbcTypeFamilyInformation.INSTANCE.locateJdbcTypeFamilyByTypeCode(jdbcTypeCode);
    if (family != null) {
        for (int potentialAlternateTypeCode : family.getTypeCodes()) {
            if (potentialAlternateTypeCode != jdbcTypeCode) {
                final JdbcType potentialAlternateDescriptor = descriptorMap.get(potentialAlternateTypeCode);
                if (potentialAlternateDescriptor != null) {
                    // todo (6.0) : add a SqlTypeDescriptor#canBeAssignedFrom method ?
                    return potentialAlternateDescriptor;
                }
                if (JdbcTypeNameMapper.isStandardTypeCode(potentialAlternateTypeCode)) {
                    log.debugf("A standard JDBC type code [%s] was not defined in SqlTypeDescriptorRegistry", potentialAlternateTypeCode);
                }
            }
        }
    }
    // finally, create a new descriptor mapping to getObject/setObject for this type code...
    final ObjectJdbcType fallBackDescriptor = new ObjectJdbcType(jdbcTypeCode);
    addDescriptor(fallBackDescriptor);
    return fallBackDescriptor;
}
Also used : ObjectJdbcType(org.hibernate.type.descriptor.jdbc.ObjectJdbcType) JdbcTypeFamilyInformation(org.hibernate.type.descriptor.jdbc.JdbcTypeFamilyInformation) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) ObjectJdbcType(org.hibernate.type.descriptor.jdbc.ObjectJdbcType)

Aggregations

ObjectJdbcType (org.hibernate.type.descriptor.jdbc.ObjectJdbcType)3 JdbcType (org.hibernate.type.descriptor.jdbc.JdbcType)2 Serializable (java.io.Serializable)1 UUID (java.util.UUID)1 MappingException (org.hibernate.MappingException)1 BasicValue (org.hibernate.mapping.BasicValue)1 Column (org.hibernate.mapping.Column)1 BasicTypeRegistry (org.hibernate.type.BasicTypeRegistry)1 SerializableType (org.hibernate.type.SerializableType)1 BasicJavaType (org.hibernate.type.descriptor.java.BasicJavaType)1 EnumJavaType (org.hibernate.type.descriptor.java.EnumJavaType)1 SerializableJavaType (org.hibernate.type.descriptor.java.SerializableJavaType)1 TemporalJavaType (org.hibernate.type.descriptor.java.TemporalJavaType)1 UUIDJavaType (org.hibernate.type.descriptor.java.UUIDJavaType)1 BinaryJdbcType (org.hibernate.type.descriptor.jdbc.BinaryJdbcType)1 JdbcTypeFamilyInformation (org.hibernate.type.descriptor.jdbc.JdbcTypeFamilyInformation)1 BasicTypeImpl (org.hibernate.type.internal.BasicTypeImpl)1 TypeConfiguration (org.hibernate.type.spi.TypeConfiguration)1 Test (org.junit.Test)1