Search in sources :

Example 6 with CustomType

use of org.hibernate.type.CustomType in project hibernate-orm by hibernate.

the class NestedEmbeddableMetadataTest method testEnumTypeInterpretation.

@Test
public void testEnumTypeInterpretation() {
    final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().enableAutoClose().applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop").build();
    try {
        final Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(Customer.class).buildMetadata();
        PersistentClass classMetadata = metadata.getEntityBinding(Customer.class.getName());
        Property investmentsProperty = classMetadata.getProperty("investments");
        Collection investmentsValue = (Collection) investmentsProperty.getValue();
        Component investmentMetadata = (Component) investmentsValue.getElement();
        Value descriptionValue = investmentMetadata.getProperty("description").getValue();
        assertEquals(1, descriptionValue.getColumnSpan());
        Column selectable = (Column) descriptionValue.getColumnIterator().next();
        assertEquals(500, selectable.getLength());
        Component amountMetadata = (Component) investmentMetadata.getProperty("amount").getValue();
        SimpleValue currencyMetadata = (SimpleValue) amountMetadata.getProperty("currency").getValue();
        CustomType currencyType = (CustomType) currencyMetadata.getType();
        int[] currencySqlTypes = currencyType.sqlTypes(metadata);
        assertEquals(1, currencySqlTypes.length);
        assertJdbcTypeCode(Types.VARCHAR, currencySqlTypes[0]);
    } finally {
        StandardServiceRegistryBuilder.destroy(serviceRegistry);
    }
}
Also used : CustomType(org.hibernate.type.CustomType) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) SimpleValue(org.hibernate.mapping.SimpleValue) Column(org.hibernate.mapping.Column) SimpleValue(org.hibernate.mapping.SimpleValue) Value(org.hibernate.mapping.Value) Collection(org.hibernate.mapping.Collection) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test)

Example 7 with CustomType

use of org.hibernate.type.CustomType in project hibernate-orm by hibernate.

the class FieldAccessedNestedEmbeddableMetadataTest method testEnumTypeInterpretation.

@Test
@FailureExpected(jiraKey = "HHH-9089")
public void testEnumTypeInterpretation() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    try {
        final Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(Customer.class).buildMetadata();
        PersistentClass classMetadata = metadata.getEntityBinding(Customer.class.getName());
        Property investmentsProperty = classMetadata.getProperty("investments");
        Collection investmentsValue = (Collection) investmentsProperty.getValue();
        Component investmentMetadata = (Component) investmentsValue.getElement();
        Value descriptionValue = investmentMetadata.getProperty("description").getValue();
        assertEquals(1, descriptionValue.getColumnSpan());
        Column selectable = (Column) descriptionValue.getColumnIterator().next();
        assertEquals(500, selectable.getLength());
        Component amountMetadata = (Component) investmentMetadata.getProperty("amount").getValue();
        SimpleValue currencyMetadata = (SimpleValue) amountMetadata.getProperty("currency").getValue();
        CustomType currencyType = (CustomType) currencyMetadata.getType();
        int[] currencySqlTypes = currencyType.sqlTypes(metadata);
        assertEquals(1, currencySqlTypes.length);
        assertJdbcTypeCode(Types.VARCHAR, currencySqlTypes[0]);
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : CustomType(org.hibernate.type.CustomType) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) SimpleValue(org.hibernate.mapping.SimpleValue) Column(org.hibernate.mapping.Column) SimpleValue(org.hibernate.mapping.SimpleValue) Value(org.hibernate.mapping.Value) Collection(org.hibernate.mapping.Collection) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Example 8 with CustomType

use of org.hibernate.type.CustomType in project hibernate-orm by hibernate.

the class QueryBuilder method toQuery.

public Query toQuery(Session session) {
    final StringBuilder querySb = new StringBuilder();
    final Map<String, Object> queryParamValues = new HashMap<>();
    build(querySb, queryParamValues);
    final Query query = session.createQuery(querySb.toString());
    for (Map.Entry<String, Object> paramValue : queryParamValues.entrySet()) {
        if (paramValue.getValue() instanceof RevisionType) {
            // this is needed when the ClassicQueryTranslatorFactory is used
            query.setParameter(paramValue.getKey(), paramValue.getValue(), new CustomType(new RevisionTypeType()));
        } else {
            query.setParameter(paramValue.getKey(), paramValue.getValue());
        }
    }
    return query;
}
Also used : CustomType(org.hibernate.type.CustomType) Query(org.hibernate.query.Query) HashMap(java.util.HashMap) RevisionType(org.hibernate.envers.RevisionType) RevisionTypeType(org.hibernate.envers.internal.entities.RevisionTypeType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with CustomType

use of org.hibernate.type.CustomType in project hibernate-orm by hibernate.

the class ExtendedEnumTypeTest method assertEnumProperty.

private void assertEnumProperty(Class<?> entityClass, Class<?> typeClass, String propertyName, EnumType expectedType) {
    doInJPA(this::entityManagerFactory, entityManager -> {
        final SessionFactoryImplementor sessionFactory = entityManager.unwrap(SessionImplementor.class).getSessionFactory();
        final EntityPersister entityPersister = sessionFactory.getMetamodel().entityPersister(entityClass);
        final EnversService enversService = sessionFactory.getServiceRegistry().getService(EnversService.class);
        final String entityName = entityPersister.getEntityName();
        final String auditEntityName = enversService.getAuditEntitiesConfiguration().getAuditEntityName(entityName);
        final EntityPersister auditedEntityPersister = sessionFactory.getMetamodel().entityPersister(auditEntityName);
        final org.hibernate.type.Type propertyType = auditedEntityPersister.getPropertyType(propertyName);
        assertTyping(CustomType.class, propertyType);
        final UserType userType = ((CustomType) propertyType).getUserType();
        assertTyping(typeClass, userType);
        assertTyping(org.hibernate.type.EnumType.class, userType);
        switch(expectedType) {
            case STRING:
                assertTrue(!((org.hibernate.type.EnumType) userType).isOrdinal());
                break;
            default:
                assertTrue(((org.hibernate.type.EnumType) userType).isOrdinal());
                break;
        }
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) CustomType(org.hibernate.type.CustomType) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EnumType(javax.persistence.EnumType) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) EnversService(org.hibernate.envers.boot.internal.EnversService) UserType(org.hibernate.usertype.UserType)

Example 10 with CustomType

use of org.hibernate.type.CustomType in project hibernate-orm by hibernate.

the class IdentifierGeneratorHelper method get.

/**
 * Extract the value from the result set (which is assumed to already have been positioned to the apopriate row)
 * and wrp it in the appropriate Java numeric type.
 *
 * @param rs The result set from which to extract the value.
 * @param identifier The name of the identifier column
 * @param type The expected type of the value.
 * @param dialect The current database dialect.
 *
 * @return The extracted value.
 *
 * @throws SQLException Indicates problems access the result set
 * @throws IdentifierGenerationException Indicates an unknown type.
 */
public static Serializable get(ResultSet rs, String identifier, Type type, Dialect dialect) throws SQLException, IdentifierGenerationException {
    if (ResultSetIdentifierConsumer.class.isInstance(type)) {
        return ((ResultSetIdentifierConsumer) type).consumeIdentifier(rs);
    }
    if (CustomType.class.isInstance(type)) {
        final CustomType customType = (CustomType) type;
        if (ResultSetIdentifierConsumer.class.isInstance(customType.getUserType())) {
            return ((ResultSetIdentifierConsumer) customType.getUserType()).consumeIdentifier(rs);
        }
    }
    ResultSetMetaData resultSetMetaData = null;
    int columnCount = 1;
    try {
        resultSetMetaData = rs.getMetaData();
        columnCount = resultSetMetaData.getColumnCount();
    } catch (Exception e) {
    // Oracle driver will throw NPE
    }
    Class clazz = type.getReturnedClass();
    if (columnCount == 1) {
        if (clazz == Long.class) {
            return rs.getLong(1);
        } else if (clazz == Integer.class) {
            return rs.getInt(1);
        } else if (clazz == Short.class) {
            return rs.getShort(1);
        } else if (clazz == String.class) {
            return rs.getString(1);
        } else if (clazz == BigInteger.class) {
            return rs.getBigDecimal(1).setScale(0, BigDecimal.ROUND_UNNECESSARY).toBigInteger();
        } else if (clazz == BigDecimal.class) {
            return rs.getBigDecimal(1).setScale(0, BigDecimal.ROUND_UNNECESSARY);
        } else {
            throw new IdentifierGenerationException("unrecognized id type : " + type.getName() + " -> " + clazz.getName());
        }
    } else {
        try {
            return extractIdentifier(rs, identifier, type, clazz);
        } catch (SQLException e) {
            if (StringHelper.isQuoted(identifier, dialect)) {
                return extractIdentifier(rs, StringHelper.unquote(identifier, dialect), type, clazz);
            }
            throw e;
        }
    }
}
Also used : CustomType(org.hibernate.type.CustomType) ResultSetMetaData(java.sql.ResultSetMetaData) BigInteger(java.math.BigInteger) SQLException(java.sql.SQLException) SQLException(java.sql.SQLException) HibernateException(org.hibernate.HibernateException) BigDecimal(java.math.BigDecimal)

Aggregations

CustomType (org.hibernate.type.CustomType)10 Test (org.junit.Test)5 Metadata (org.hibernate.boot.Metadata)4 MetadataSources (org.hibernate.boot.MetadataSources)4 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)3 Column (org.hibernate.mapping.Column)3 PersistentClass (org.hibernate.mapping.PersistentClass)3 Property (org.hibernate.mapping.Property)3 EnumType (javax.persistence.EnumType)2 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 Collection (org.hibernate.mapping.Collection)2 Component (org.hibernate.mapping.Component)2 SimpleValue (org.hibernate.mapping.SimpleValue)2 Value (org.hibernate.mapping.Value)2 EntityPersister (org.hibernate.persister.entity.EntityPersister)2 EnumType (org.hibernate.type.EnumType)2 Type (org.hibernate.type.Type)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1