org.hibernate.usertype.UserType Java Examples

The following examples show how to use org.hibernate.usertype.UserType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: lams   Author: lamsfoundation   File: TypeFactory.java    License: GNU General Public License v2.0 6 votes vote down vote up
public Type byClass(Class clazz, Properties parameters) {
	if ( Type.class.isAssignableFrom( clazz ) ) {
		return type( clazz, parameters );
	}

	if ( CompositeUserType.class.isAssignableFrom( clazz ) ) {
		return customComponent( clazz, parameters );
	}

	if ( UserType.class.isAssignableFrom( clazz ) ) {
		return custom( clazz, parameters );
	}

	if ( Lifecycle.class.isAssignableFrom( clazz ) ) {
		// not really a many-to-one association *necessarily*
		return manyToOne( clazz.getName() );
	}

	if ( Serializable.class.isAssignableFrom( clazz ) ) {
		return serializable( clazz );
	}

	return null;
}
 
Example #2
Source Project: lams   Author: lamsfoundation   File: TypeFactory.java    License: GNU General Public License v2.0 5 votes vote down vote up
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 );
	}
}
 
Example #3
Source Project: lams   Author: lamsfoundation   File: TypeFactory.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @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 );
	}
}
 
Example #4
Source Project: lams   Author: lamsfoundation   File: CustomType.java    License: GNU General Public License v2.0 5 votes vote down vote up
public CustomType(UserType userType, String[] registrationKeys) throws MappingException {
	this.userType = userType;
	this.name = userType.getClass().getName();
	this.types = userType.sqlTypes();
	this.dictatedSizes = Sized.class.isInstance( userType )
			? ( (Sized) userType ).dictatedSizes()
			: new Size[ types.length ];
	this.defaultSizes = Sized.class.isInstance( userType )
			? ( (Sized) userType ).defaultSizes()
			: new Size[ types.length ];
	this.customLogging = LoggableUserType.class.isInstance( userType );
	this.registrationKeys = registrationKeys;
}
 
Example #5
Source Project: hibernate-types   Author: vladmihalcea   File: AbstractTest.java    License: Apache License 2.0 5 votes vote down vote up
protected EntityManagerFactory newEntityManagerFactory() {
    PersistenceUnitInfo persistenceUnitInfo = persistenceUnitInfo(getClass().getSimpleName());
    Map<String, Object> configuration = new HashMap<>();
    configuration.put(AvailableSettings.INTERCEPTOR, interceptor());
    Integrator integrator = integrator();
    if (integrator != null) {
        configuration.put("hibernate.integrator_provider", (IntegratorProvider) () -> Collections.singletonList(integrator));
    }

    final List<Type> additionalTypes = additionalTypes();
    if (additionalTypes != null) {
        configuration.put("hibernate.type_contributors", (TypeContributorList) () -> {
            List<TypeContributor> typeContributors = new ArrayList<>();

            for (Type additionalType : additionalTypes) {
                if (additionalType instanceof BasicType) {
                    typeContributors.add((typeContributions, serviceRegistry) -> typeContributions.contributeType((BasicType) additionalType));


                } else if (additionalType instanceof UserType) {
                    typeContributors.add((typeContributions, serviceRegistry) -> typeContributions.contributeType((UserType) additionalType));
                } else if (additionalType instanceof CompositeUserType) {
                    typeContributors.add((typeContributions, serviceRegistry) -> typeContributions.contributeType((CompositeUserType) additionalType));
                }
            }
            return typeContributors;
        });
    }

    EntityManagerFactoryBuilderImpl entityManagerFactoryBuilder = new EntityManagerFactoryBuilderImpl(
            new PersistenceUnitInfoDescriptor(persistenceUnitInfo), configuration
    );
    return entityManagerFactoryBuilder.build();
}
 
Example #6
Source Project: cacheonix-core   Author: cacheonix   File: CustomType.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
public CustomType(Class userTypeClass, Properties parameters) throws MappingException {

		if ( !UserType.class.isAssignableFrom( userTypeClass ) ) {
			throw new MappingException(
					"Custom type does not implement UserType: " +
					userTypeClass.getName()
				);
		}

		name = userTypeClass.getName();

		try {
			userType = ( UserType ) userTypeClass.newInstance();
		}
		catch ( InstantiationException ie ) {
			throw new MappingException(
					"Cannot instantiate custom type: " +
					userTypeClass.getName()
				);
		}
		catch ( IllegalAccessException iae ) {
			throw new MappingException(
					"IllegalAccessException trying to instantiate custom type: " +
					userTypeClass.getName()
				);
		}

        TypeFactory.injectParameters( userType, parameters );
		types = userType.sqlTypes();

		customLogging = LoggableUserType.class.isAssignableFrom( userTypeClass );
	}
 
Example #7
Source Project: lams   Author: lamsfoundation   File: Configuration.java    License: GNU General Public License v2.0 4 votes vote down vote up
public Configuration registerTypeOverride(UserType type, String[] keys) {
	basicTypes.add( new CustomType( type, keys ) );
	return this;
}
 
Example #8
Source Project: lams   Author: lamsfoundation   File: AbstractDelegatingMetadataBuilderImplementor.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public MetadataBuilder applyBasicType(UserType type, String... keys) {
	delegate.applyBasicType( type, keys );
	return getThis();
}
 
Example #9
Source Project: lams   Author: lamsfoundation   File: BasicTypeRegistration.java    License: GNU General Public License v2.0 4 votes vote down vote up
public BasicTypeRegistration(UserType type, String[] keys) {
	this( new CustomType( type, keys ), keys );
}
 
Example #10
Source Project: lams   Author: lamsfoundation   File: MetadataBuilderImpl.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public MetadataBuilder applyBasicType(UserType type, String... keys) {
	options.basicTypeRegistrations.add( new BasicTypeRegistration( type, keys ) );
	return this;
}
 
Example #11
Source Project: lams   Author: lamsfoundation   File: MetadataBuilderImpl.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public void contributeType(UserType type, String[] keys) {
	options.basicTypeRegistrations.add( new BasicTypeRegistration( type, keys ) );
}
 
Example #12
Source Project: lams   Author: lamsfoundation   File: TypeResolver.java    License: GNU General Public License v2.0 4 votes vote down vote up
public void registerTypeOverride(UserType type, String[] keys) {
	typeConfiguration.getBasicTypeRegistry().register( type, keys );
}
 
Example #13
Source Project: lams   Author: lamsfoundation   File: CustomType.java    License: GNU General Public License v2.0 4 votes vote down vote up
public CustomType(UserType userType) throws MappingException {
	this( userType, ArrayHelper.EMPTY_STRING_ARRAY );
}
 
Example #14
Source Project: lams   Author: lamsfoundation   File: CustomType.java    License: GNU General Public License v2.0 4 votes vote down vote up
public UserType getUserType() {
	return userType;
}
 
Example #15
Source Project: lams   Author: lamsfoundation   File: BasicTypeRegistry.java    License: GNU General Public License v2.0 4 votes vote down vote up
public void register(UserType type, String[] keys) {
	register( new CustomType( type, keys ) );
}
 
Example #16
Source Project: hibernate-types   Author: vladmihalcea   File: AbstractTest.java    License: Apache License 2.0 4 votes vote down vote up
private SessionFactory newSessionFactory() {
    final BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
            .enableAutoClose();

    Integrator integrator = integrator();
    if (integrator != null) {
        bsrb.applyIntegrator(integrator);
    }

    final BootstrapServiceRegistry bsr = bsrb.build();

    final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(bsr)
            .applySettings(properties())
            .build();

    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);

    for (Class annotatedClass : entities()) {
        metadataSources.addAnnotatedClass(annotatedClass);
    }

    String[] packages = packages();
    if (packages != null) {
        for (String annotatedPackage : packages) {
            metadataSources.addPackage(annotatedPackage);
        }
    }

    String[] resources = resources();
    if (resources != null) {
        for (String resource : resources) {
            metadataSources.addResource(resource);
        }
    }

    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
    metadataBuilder.enableNewIdentifierGeneratorSupport(true);
    metadataBuilder.applyImplicitNamingStrategy(ImplicitNamingStrategyLegacyJpaImpl.INSTANCE);

    final List<Type> additionalTypes = additionalTypes();
    if (additionalTypes != null) {
        additionalTypes.stream().forEach(type -> {
            metadataBuilder.applyTypes((typeContributions, serviceRegistry1) -> {
                if(type instanceof BasicType) {
                    typeContributions.contributeType((BasicType) type);
                } else if (type instanceof UserType ){
                    typeContributions.contributeType((UserType) type);
                } else if (type instanceof CompositeUserType) {
                    typeContributions.contributeType((CompositeUserType) type);
                }
            });
        });
    }

    MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build();

    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    Interceptor interceptor = interceptor();
    if (interceptor != null) {
        sfb.applyInterceptor(interceptor);
    }

    return sfb.build();
}
 
Example #17
Source Project: hibernate-types   Author: vladmihalcea   File: AbstractTest.java    License: Apache License 2.0 4 votes vote down vote up
private SessionFactory newLegacySessionFactory() {
    Properties properties = properties();
    Configuration configuration = new Configuration().addProperties(properties);
    for (Class<?> entityClass : entities()) {
        configuration.addAnnotatedClass(entityClass);
    }
    String[] packages = packages();
    if (packages != null) {
        for (String scannedPackage : packages) {
            configuration.addPackage(scannedPackage);
        }
    }
    String[] resources = resources();
    if (resources != null) {
        for (String resource : resources) {
            configuration.addResource(resource);
        }
    }
    Interceptor interceptor = interceptor();
    if (interceptor != null) {
        configuration.setInterceptor(interceptor);
    }

    final List<Type> additionalTypes = additionalTypes();
    if (additionalTypes != null) {
        configuration.registerTypeContributor((typeContributions, serviceRegistry) -> {
            additionalTypes.stream().forEach(type -> {
                if (type instanceof BasicType) {
                    typeContributions.contributeType((BasicType) type);
                } else if (type instanceof UserType) {
                    typeContributions.contributeType((UserType) type);
                } else if (type instanceof CompositeUserType) {
                    typeContributions.contributeType((CompositeUserType) type);
                }
            });
        });
    }
    return configuration.buildSessionFactory(
            new StandardServiceRegistryBuilder()
                    .applySettings(properties)
                    .build()
    );
}
 
Example #18
Source Project: hibernate-types   Author: vladmihalcea   File: AbstractTest.java    License: Apache License 2.0 4 votes vote down vote up
private SessionFactory newSessionFactory() {
    Properties properties = properties();
    Configuration configuration = new Configuration().addProperties(properties);
    for (Class<?> entityClass : entities()) {
        configuration.addAnnotatedClass(entityClass);
    }
    String[] packages = packages();
    if (packages != null) {
        for (String scannedPackage : packages) {
            configuration.addPackage(scannedPackage);
        }
    }
    String[] resources = resources();
    if (resources != null) {
        for (String resource : resources) {
            configuration.addResource(resource);
        }
    }
    Interceptor interceptor = interceptor();
    if (interceptor != null) {
        configuration.setInterceptor(interceptor);
    }

    final List<Type> additionalTypes = additionalTypes();
    if (additionalTypes != null) {
        configuration.registerTypeContributor(new TypeContributor() {
            @Override
            public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
                for (Type type : additionalTypes) {
                    if (type instanceof BasicType) {
                        typeContributions.contributeType((BasicType) type);
                    } else if (type instanceof UserType) {
                        typeContributions.contributeType((UserType) type, new String[]{type.getName()});
                    } else if (type instanceof CompositeUserType) {
                        typeContributions.contributeType((CompositeUserType) type, new String[]{type.getName()});
                    }
                }
            }
        });
    }
    return configuration.buildSessionFactory(
            new StandardServiceRegistryBuilder()
                    .applySettings(properties)
                    .build()
    );
}
 
Example #19
Source Project: hibernate-types   Author: vladmihalcea   File: AbstractTest.java    License: Apache License 2.0 4 votes vote down vote up
private SessionFactory newLegacySessionFactory() {
    Properties properties = properties();
    Configuration configuration = new Configuration().addProperties(properties);
    for (Class<?> entityClass : entities()) {
        configuration.addAnnotatedClass(entityClass);
    }
    String[] packages = packages();
    if (packages != null) {
        for (String scannedPackage : packages) {
            configuration.addPackage(scannedPackage);
        }
    }
    String[] resources = resources();
    if (resources != null) {
        for (String resource : resources) {
            configuration.addResource(resource);
        }
    }
    Interceptor interceptor = interceptor();
    if (interceptor != null) {
        configuration.setInterceptor(interceptor);
    }

    final List<Type> additionalTypes = additionalTypes();
    if (additionalTypes != null) {
        configuration.registerTypeContributor(new TypeContributor() {
            @Override
            public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
                for (Type type : additionalTypes) {
                    if (type instanceof BasicType) {
                        typeContributions.contributeType((BasicType) type);
                    } else if (type instanceof UserType) {
                        typeContributions.contributeType((UserType) type);
                    } else if (type instanceof CompositeUserType) {
                        typeContributions.contributeType((CompositeUserType) type);
                    }
                }
            }
        });
    }
    return configuration.buildSessionFactory(
            new StandardServiceRegistryBuilder()
                    .applySettings(properties)
                    .build()
    );
}
 
Example #20
Source Project: cacheonix-core   Author: cacheonix   File: TypeFactory.java    License: GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Uses heuristics to deduce a Hibernate type given a string naming the type or Java class.
 * Return an instance of <tt>org.hibernate.type.Type</tt>.
 */
public static Type heuristicType(String typeName, Properties parameters)
		throws MappingException {
	Type type = TypeFactory.basic( typeName );
	if ( type == null ) {
		Class typeClass;
		try {
			typeClass = ReflectHelper.classForName( typeName );
		}
		catch (ClassNotFoundException cnfe) {
			typeClass = null;
		}
		if ( typeClass != null ) {
			if ( Type.class.isAssignableFrom( typeClass ) ) {
				try {
					type = (Type) typeClass.newInstance();
				}
				catch (Exception e) {
					throw new MappingException( 
							"Could not instantiate Type: " + typeClass.getName(),
							e 
						);
				}
				injectParameters(type, parameters);
			}
			else if ( CompositeUserType.class.isAssignableFrom( typeClass ) ) {
				type = new CompositeCustomType( typeClass, parameters );
			}
			else if ( UserType.class.isAssignableFrom( typeClass ) ) {
				type = new CustomType( typeClass, parameters );
			}
			else if ( Lifecycle.class.isAssignableFrom( typeClass ) ) {
				type = Hibernate.entity( typeClass );
			}
			else if ( Serializable.class.isAssignableFrom( typeClass ) ) {
				type = Hibernate.serializable( typeClass );
			}
		}
	}
	return type;

}
 
Example #21
Source Project: high-performance-java-persistence   Author: vladmihalcea   File: AbstractTest.java    License: Apache License 2.0 4 votes vote down vote up
private SessionFactory newSessionFactory() {
    final BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
        .enableAutoClose();

    Integrator integrator = integrator();
    if (integrator != null) {
        bsrb.applyIntegrator( integrator );
    }

    final BootstrapServiceRegistry bsr = bsrb.build();

    final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(bsr)
        .applySettings(properties())
        .build();

    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);

    for (Class annotatedClass : entities()) {
        metadataSources.addAnnotatedClass(annotatedClass);
    }

    String[] packages = packages();
    if (packages != null) {
        for (String annotatedPackage : packages) {
            metadataSources.addPackage(annotatedPackage);
        }
    }

    String[] resources = resources();
    if (resources != null) {
        for (String resource : resources) {
            metadataSources.addResource(resource);
        }
    }

    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder()
    .enableNewIdentifierGeneratorSupport(true)
    .applyImplicitNamingStrategy(ImplicitNamingStrategyLegacyJpaImpl.INSTANCE);

    final List<Type> additionalTypes = additionalTypes();
    if (additionalTypes != null) {
        additionalTypes.stream().forEach(type -> {
            metadataBuilder.applyTypes((typeContributions, sr) -> {
                if(type instanceof BasicType) {
                    typeContributions.contributeType((BasicType) type);
                } else if (type instanceof UserType ){
                    typeContributions.contributeType((UserType) type);
                } else if (type instanceof CompositeUserType) {
                    typeContributions.contributeType((CompositeUserType) type);
                }
            });
        });
    }

    additionalMetadata(metadataBuilder);

    MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build();

    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    Interceptor interceptor = interceptor();
    if(interceptor != null) {
        sfb.applyInterceptor(interceptor);
    }

    return sfb.build();
}
 
Example #22
Source Project: high-performance-java-persistence   Author: vladmihalcea   File: AbstractTest.java    License: Apache License 2.0 4 votes vote down vote up
private SessionFactory newLegacySessionFactory() {
    Properties properties = properties();
    Configuration configuration = new Configuration().addProperties(properties);
    for(Class<?> entityClass : entities()) {
        configuration.addAnnotatedClass(entityClass);
    }
    String[] packages = packages();
    if(packages != null) {
        for(String scannedPackage : packages) {
            configuration.addPackage(scannedPackage);
        }
    }
    String[] resources = resources();
    if (resources != null) {
        for (String resource : resources) {
            configuration.addResource(resource);
        }
    }
    Interceptor interceptor = interceptor();
    if(interceptor != null) {
        configuration.setInterceptor(interceptor);
    }

    final List<Type> additionalTypes = additionalTypes();
    if (additionalTypes != null) {
        configuration.registerTypeContributor((typeContributions, serviceRegistry) -> {
            additionalTypes.stream().forEach(type -> {
                if(type instanceof BasicType) {
                    typeContributions.contributeType((BasicType) type);
                } else if (type instanceof UserType ){
                    typeContributions.contributeType((UserType) type);
                } else if (type instanceof CompositeUserType) {
                    typeContributions.contributeType((CompositeUserType) type);
                }
            });
        });
    }
    return configuration.buildSessionFactory(
            new StandardServiceRegistryBuilder()
                    .applySettings(properties)
                    .build()
    );
}
 
Example #23
Source Project: jadira   Author: JadiraOrg   File: AbstractUserTypeHibernateIntegrator.java    License: Apache License 2.0 4 votes vote down vote up
private void registerType(Configuration configuration, UserType type) {
	String className = type.returnedClass().getName();
	configuration.registerTypeOverride(type, new String[] {className});
}
 
Example #24
Source Project: jadira   Author: JadiraOrg   File: AbstractUserTypeHibernateIntegrator.java    License: Apache License 2.0 4 votes vote down vote up
private void registerType(MetadataImplementor mi, UserType type) {
	String className = type.returnedClass().getName();
	mi.getTypeResolver().registerTypeOverride(type, new String[] {className});
}
 
Example #25
Source Project: jadira   Author: JadiraOrg   File: UserTypeJodaTimeHibernateIntegrator.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected UserType[] getUserTypes() {
	return userTypes;
}
 
Example #26
Source Project: jadira   Author: JadiraOrg   File: UserTypeThreeTenHibernateIntegrator.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected UserType[] getUserTypes() {
	return userTypes;
}
 
Example #27
Source Project: jadira   Author: JadiraOrg   File: UserTypeJodaMoneyHibernateIntegrator.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected UserType[] getUserTypes() {
	return userTypes;
}
 
Example #28
Source Project: jadira   Author: JadiraOrg   File: UserTypeMonetaMoneyHibernateIntegrator.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected UserType[] getUserTypes() {
	return userTypes;
}
 
Example #29
Source Project: jadira   Author: JadiraOrg   File: UserTypeLegacyJdkMoneyHibernateIntegrator.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected UserType[] getUserTypes() {
	return userTypes;
}
 
Example #30
Source Project: geomajas-project-server   Author: geomajas   File: HSQLSpatialDialect.java    License: GNU Affero General Public License v3.0 4 votes vote down vote up
public UserType getGeometryUserType() {
	return new HsqlGeometryUserType();
}