Search in sources :

Example 1 with UserType

use of org.hibernate.usertype.UserType in project hibernate-orm by hibernate.

the class MetadataBuildingProcess method handleTypes.

//	private static JandexInitManager buildJandexInitializer(
//			MetadataBuildingOptions options,
//			ClassLoaderAccess classLoaderAccess) {
//		final boolean autoIndexMembers = ConfigurationHelper.getBoolean(
//				org.hibernate.cfg.AvailableSettings.ENABLE_AUTO_INDEX_MEMBER_TYPES,
//				options.getServiceRegistry().getService( ConfigurationService.class ).getSettings(),
//				false
//		);
//
//		return new JandexInitManager( options.getJandexView(), classLoaderAccess, autoIndexMembers );
//	}
private static BasicTypeRegistry handleTypes(MetadataBuildingOptions options) {
    final ClassLoaderService classLoaderService = options.getServiceRegistry().getService(ClassLoaderService.class);
    // ultimately this needs to change a little bit to account for HHH-7792
    final BasicTypeRegistry basicTypeRegistry = new BasicTypeRegistry();
    final TypeContributions typeContributions = new TypeContributions() {

        @Override
        public void contributeType(org.hibernate.type.BasicType type) {
            basicTypeRegistry.register(type);
        }

        @Override
        public void contributeType(BasicType type, String... keys) {
            basicTypeRegistry.register(type, keys);
        }

        @Override
        public void contributeType(UserType type, String[] keys) {
            basicTypeRegistry.register(type, keys);
        }

        @Override
        public void contributeType(CompositeUserType type, String[] keys) {
            basicTypeRegistry.register(type, keys);
        }
    };
    // add Dialect contributed types
    final Dialect dialect = options.getServiceRegistry().getService(JdbcServices.class).getDialect();
    dialect.contributeTypes(typeContributions, options.getServiceRegistry());
    // add TypeContributor contributed types.
    for (TypeContributor contributor : classLoaderService.loadJavaServices(TypeContributor.class)) {
        contributor.contribute(typeContributions, options.getServiceRegistry());
    }
    // add explicit application registered types
    for (BasicTypeRegistration basicTypeRegistration : options.getBasicTypeRegistrations()) {
        basicTypeRegistry.register(basicTypeRegistration.getBasicType(), basicTypeRegistration.getRegistrationKeys());
    }
    return basicTypeRegistry;
}
Also used : BasicType(org.hibernate.type.BasicType) TypeContributions(org.hibernate.boot.model.TypeContributions) Dialect(org.hibernate.dialect.Dialect) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) BasicTypeRegistry(org.hibernate.type.BasicTypeRegistry) BasicTypeRegistration(org.hibernate.boot.spi.BasicTypeRegistration) CompositeUserType(org.hibernate.usertype.CompositeUserType) UserType(org.hibernate.usertype.UserType) CompositeUserType(org.hibernate.usertype.CompositeUserType) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) TypeContributor(org.hibernate.boot.model.TypeContributor)

Example 2 with UserType

use of org.hibernate.usertype.UserType in project hibernate-orm by hibernate.

the class MetadataBuildingProcess method handleTypes.

// todo (7.0) : buildJandexInitializer
// private static JandexInitManager buildJandexInitializer(
// MetadataBuildingOptions options,
// ClassLoaderAccess classLoaderAccess) {
// final boolean autoIndexMembers = ConfigurationHelper.getBoolean(
// org.hibernate.cfg.AvailableSettings.ENABLE_AUTO_INDEX_MEMBER_TYPES,
// options.getServiceRegistry().getService( ConfigurationService.class ).getSettings(),
// false
// );
// 
// return new JandexInitManager( options.getJandexView(), classLoaderAccess, autoIndexMembers );
// }
private static void handleTypes(BootstrapContext bootstrapContext, MetadataBuildingOptions options) {
    final ClassLoaderService classLoaderService = options.getServiceRegistry().getService(ClassLoaderService.class);
    final TypeContributions typeContributions = new TypeContributions() {

        @Override
        public void contributeType(org.hibernate.type.BasicType type) {
            getBasicTypeRegistry().register(type);
        }

        @Override
        public void contributeType(BasicType type, String... keys) {
            getBasicTypeRegistry().register(type, keys);
        }

        @Override
        public void contributeType(UserType type, String[] keys) {
            getBasicTypeRegistry().register(type, keys);
        }

        @Override
        public void contributeType(CompositeUserType type, String[] keys) {
            getBasicTypeRegistry().register(type, keys);
        }

        @Override
        public void contributeJavaTypeDescriptor(JavaTypeDescriptor descriptor) {
            bootstrapContext.getTypeConfiguration().getJavaTypeDescriptorRegistry().addDescriptor(descriptor);
        }

        @Override
        public void contributeSqlTypeDescriptor(SqlTypeDescriptor descriptor) {
            bootstrapContext.getTypeConfiguration().getSqlTypeDescriptorRegistry().addDescriptor(descriptor);
        }

        @Override
        public TypeConfiguration getTypeConfiguration() {
            return bootstrapContext.getTypeConfiguration();
        }

        final BasicTypeRegistry getBasicTypeRegistry() {
            return bootstrapContext.getTypeConfiguration().getBasicTypeRegistry();
        }
    };
    // add Dialect contributed types
    final Dialect dialect = options.getServiceRegistry().getService(JdbcServices.class).getDialect();
    dialect.contributeTypes(typeContributions, options.getServiceRegistry());
    // add TypeContributor contributed types.
    for (TypeContributor contributor : classLoaderService.loadJavaServices(TypeContributor.class)) {
        contributor.contribute(typeContributions, options.getServiceRegistry());
    }
    // add explicit application registered types
    for (BasicTypeRegistration basicTypeRegistration : options.getBasicTypeRegistrations()) {
        bootstrapContext.getTypeConfiguration().getBasicTypeRegistry().register(basicTypeRegistration.getBasicType(), basicTypeRegistration.getRegistrationKeys());
    }
}
Also used : BasicType(org.hibernate.type.BasicType) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) BasicTypeRegistration(org.hibernate.boot.spi.BasicTypeRegistration) JavaTypeDescriptor(org.hibernate.type.descriptor.java.JavaTypeDescriptor) TypeContributor(org.hibernate.boot.model.TypeContributor) SqlTypeDescriptor(org.hibernate.type.descriptor.sql.SqlTypeDescriptor) TypeContributions(org.hibernate.boot.model.TypeContributions) Dialect(org.hibernate.dialect.Dialect) CompositeUserType(org.hibernate.usertype.CompositeUserType) UserType(org.hibernate.usertype.UserType) CompositeUserType(org.hibernate.usertype.CompositeUserType) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService)

Example 3 with UserType

use of org.hibernate.usertype.UserType 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 4 with UserType

use of org.hibernate.usertype.UserType in project hibernate-orm by hibernate.

the class TypeFactory method custom.

/**
 * @deprecated Only for use temporary use by {@link org.hibernate.Hibernate}
 */
@Deprecated
public static CustomType custom(Class<UserType> typeClass, Properties parameters, TypeScope scope) {
    try {
        UserType userType = typeClass.newInstance();
        injectParameters(userType, parameters);
        return new CustomType(userType);
    } catch (Exception e) {
        throw new MappingException("Unable to instantiate custom type: " + typeClass.getName(), e);
    }
}
Also used : CompositeUserType(org.hibernate.usertype.CompositeUserType) UserType(org.hibernate.usertype.UserType) MappingException(org.hibernate.MappingException) MappingException(org.hibernate.MappingException)

Example 5 with UserType

use of org.hibernate.usertype.UserType in project hibernate-orm by hibernate.

the class TypeFactory method custom.

public CustomType custom(Class<UserType> typeClass, Properties parameters) {
    try {
        UserType userType = typeClass.newInstance();
        if (TypeConfigurationAware.class.isInstance(userType)) {
            ((TypeConfigurationAware) userType).setTypeConfiguration(typeConfiguration);
        }
        injectParameters(userType, parameters);
        return new CustomType(userType);
    } catch (Exception e) {
        throw new MappingException("Unable to instantiate custom type: " + typeClass.getName(), e);
    }
}
Also used : TypeConfigurationAware(org.hibernate.type.spi.TypeConfigurationAware) CompositeUserType(org.hibernate.usertype.CompositeUserType) UserType(org.hibernate.usertype.UserType) MappingException(org.hibernate.MappingException) MappingException(org.hibernate.MappingException)

Aggregations

UserType (org.hibernate.usertype.UserType)5 CompositeUserType (org.hibernate.usertype.CompositeUserType)4 MappingException (org.hibernate.MappingException)2 TypeContributions (org.hibernate.boot.model.TypeContributions)2 TypeContributor (org.hibernate.boot.model.TypeContributor)2 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)2 BasicTypeRegistration (org.hibernate.boot.spi.BasicTypeRegistration)2 Dialect (org.hibernate.dialect.Dialect)2 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)2 BasicType (org.hibernate.type.BasicType)2 EnumType (javax.persistence.EnumType)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)1 EnversService (org.hibernate.envers.boot.internal.EnversService)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 BasicTypeRegistry (org.hibernate.type.BasicTypeRegistry)1 CustomType (org.hibernate.type.CustomType)1 JavaTypeDescriptor (org.hibernate.type.descriptor.java.JavaTypeDescriptor)1 SqlTypeDescriptor (org.hibernate.type.descriptor.sql.SqlTypeDescriptor)1 TypeConfigurationAware (org.hibernate.type.spi.TypeConfigurationAware)1