下面列出了怎么用org.hibernate.type.BasicType的API类实例代码及写法,或者点击链接到github查看源代码。
private void resolveLob(final SingularAttributeSourceBasic attributeSource, SimpleValue value) {
// Resolves whether the property is LOB based on the type attribute on the attribute property source.
// Essentially this expects the type to map to a CLOB/NCLOB/BLOB sql type internally and compares.
if ( !value.isLob() && value.getTypeName() != null ) {
final TypeResolver typeResolver = attributeSource.getBuildingContext().getMetadataCollector().getTypeResolver();
final BasicType basicType = typeResolver.basic( value.getTypeName() );
if ( basicType instanceof AbstractSingleColumnStandardBasicType ) {
if ( isLob( ( (AbstractSingleColumnStandardBasicType) basicType ).getSqlTypeDescriptor().getSqlType(), null ) ) {
value.makeLob();
}
}
}
// If the prior check didn't set the lob flag, this will inspect the column sql-type attribute value and
// if this maps to CLOB/NCLOB/BLOB then the value will be marked as lob.
if ( !value.isLob() ) {
for ( RelationalValueSource relationalValueSource : attributeSource.getRelationalValueSources() ) {
if ( ColumnSource.class.isInstance( relationalValueSource ) ) {
if ( isLob( null, ( (ColumnSource) relationalValueSource ).getSqlType() ) ) {
value.makeLob();
}
}
}
}
}
public BasicType resolveTimestampTemporalTypeVariant(Class javaType, Type baseType) {
// prefer to use any Type already known - interprets TIMESTAMP as "no narrowing"
if ( baseType != null && baseType instanceof BasicType ) {
return (BasicType) baseType;
}
if ( Calendar.class.isAssignableFrom( javaType ) ) {
return CalendarType.INSTANCE;
}
if ( java.util.Date.class.isAssignableFrom( javaType ) ) {
return TimestampType.INSTANCE;
}
if ( Instant.class.isAssignableFrom( javaType ) ) {
return InstantType.INSTANCE;
}
if ( OffsetDateTime.class.isAssignableFrom( javaType ) ) {
return OffsetDateTimeType.INSTANCE;
}
if ( ZonedDateTime.class.isAssignableFrom( javaType ) ) {
return ZonedDateTimeType.INSTANCE;
}
if ( OffsetTime.class.isAssignableFrom( javaType ) ) {
return OffsetTimeType.INSTANCE;
}
throw new IllegalArgumentException( "Unsure how to handle given Java type [" + javaType.getName() + "] as TemporalType#TIMESTAMP" );
}
@SuppressWarnings("unchecked")
public BasicType resolveDateTemporalTypeVariant(Class javaType, Type baseType) {
// prefer to use any Type already known
if ( baseType != null && baseType instanceof BasicType ) {
if ( baseType.getReturnedClass().isAssignableFrom( javaType ) ) {
return (BasicType) baseType;
}
}
if ( Calendar.class.isAssignableFrom( javaType ) ) {
return CalendarDateType.INSTANCE;
}
if ( java.util.Date.class.isAssignableFrom( javaType ) ) {
return TimestampType.INSTANCE;
}
if ( Instant.class.isAssignableFrom( javaType ) ) {
return OffsetDateTimeType.INSTANCE;
}
if ( OffsetDateTime.class.isAssignableFrom( javaType ) ) {
return OffsetDateTimeType.INSTANCE;
}
if ( ZonedDateTime.class.isAssignableFrom( javaType ) ) {
return ZonedDateTimeType.INSTANCE;
}
throw new IllegalArgumentException( "Unsure how to handle given Java type [" + javaType.getName() + "] as TemporalType#DATE" );
}
public BasicType resolveTimeTemporalTypeVariant(Class javaType, Type baseType) {
if ( Calendar.class.isAssignableFrom( javaType ) ) {
return CalendarTimeType.INSTANCE;
}
if ( java.util.Date.class.isAssignableFrom( javaType ) ) {
return TimestampType.INSTANCE;
}
throw new IllegalArgumentException( "Unsure how to handle given Java type [" + javaType.getName() + "] as TemporalType#TIME" );
}
public ExportableColumn(Database database, Table table, String name, BasicType type) {
this(
database,
table,
name,
type,
database.getDialect().getTypeName( type.sqlTypes( null )[0] )
);
}
public ExportableColumn(
Database database,
Table table,
String name,
BasicType type,
String dbTypeDeclaration) {
super( name );
setValue( new ValueImpl( this, table, type, database ) );
setSqlType( dbTypeDeclaration );
}
@Override
public BasicType basic(Class javaType) {
BasicType type = typeResolver.basic( javaType.getName() );
if ( type == null ) {
final Class variant = resolvePrimitiveOrPrimitiveWrapperVariantJavaType( javaType );
if ( variant != null ) {
type = typeResolver.basic( variant.getName() );
}
}
return type;
}
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();
}
public final void typeDefinedNoRegistrationKeys(final BasicType arg0) {
super.log.logf(FQCN, (org.jboss.logging.Logger.Level.WARN), null, typeDefinedNoRegistrationKeys$str(), arg0);
}
public final void typeDefinedNoRegistrationKeys(BasicType arg0) {
super.log.logf(FQCN, Logger.Level.WARN, (Throwable)null, this.typeDefinedNoRegistrationKeys$str(), arg0);
}
/**
* Create a {@link SessionFactory} using the properties and mappings in this configuration. The
* SessionFactory will be immutable, so changes made to this Configuration after building the
* SessionFactory will not affect it.
*
* @param serviceRegistry The registry of services to be used in creating this session factory.
*
* @return The built {@link SessionFactory}
*
* @throws HibernateException usually indicates an invalid configuration or invalid mapping information
*/
public SessionFactory buildSessionFactory(ServiceRegistry serviceRegistry) throws HibernateException {
log.debug( "Building session factory using provided StandardServiceRegistry" );
final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder( (StandardServiceRegistry) serviceRegistry );
if ( implicitNamingStrategy != null ) {
metadataBuilder.applyImplicitNamingStrategy( implicitNamingStrategy );
}
if ( physicalNamingStrategy != null ) {
metadataBuilder.applyPhysicalNamingStrategy( physicalNamingStrategy );
}
if ( sharedCacheMode != null ) {
metadataBuilder.applySharedCacheMode( sharedCacheMode );
}
if ( !typeContributorRegistrations.isEmpty() ) {
for ( TypeContributor typeContributor : typeContributorRegistrations ) {
metadataBuilder.applyTypes( typeContributor );
}
}
if ( !basicTypes.isEmpty() ) {
for ( BasicType basicType : basicTypes ) {
metadataBuilder.applyBasicType( basicType );
}
}
if ( sqlFunctions != null ) {
for ( Map.Entry<String, SQLFunction> entry : sqlFunctions.entrySet() ) {
metadataBuilder.applySqlFunction( entry.getKey(), entry.getValue() );
}
}
if ( auxiliaryDatabaseObjectList != null ) {
for ( AuxiliaryDatabaseObject auxiliaryDatabaseObject : auxiliaryDatabaseObjectList ) {
metadataBuilder.applyAuxiliaryDatabaseObject( auxiliaryDatabaseObject );
}
}
if ( attributeConverterDefinitionsByClass != null ) {
for ( AttributeConverterDefinition attributeConverterDefinition : attributeConverterDefinitionsByClass.values() ) {
metadataBuilder.applyAttributeConverter( attributeConverterDefinition );
}
}
final Metadata metadata = metadataBuilder.build();
final SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
if ( interceptor != null && interceptor != EmptyInterceptor.INSTANCE ) {
sessionFactoryBuilder.applyInterceptor( interceptor );
}
if ( getSessionFactoryObserver() != null ) {
sessionFactoryBuilder.addSessionFactoryObservers( getSessionFactoryObserver() );
}
if ( getEntityNotFoundDelegate() != null ) {
sessionFactoryBuilder.applyEntityNotFoundDelegate( getEntityNotFoundDelegate() );
}
if ( getEntityTuplizerFactory() != null ) {
sessionFactoryBuilder.applyEntityTuplizerFactory( getEntityTuplizerFactory() );
}
if ( getCurrentTenantIdentifierResolver() != null ) {
sessionFactoryBuilder.applyCurrentTenantIdentifierResolver( getCurrentTenantIdentifierResolver() );
}
return sessionFactoryBuilder.build();
}
public BasicType determineTypeForTemporalType(TemporalType temporalType, Type baseType, Object bindValue) {
// todo : for 6.0 make TemporalType part of org.hibernate.type.descriptor.java.JdbcRecommendedSqlTypeMappingContext
// then we can just ask the org.hibernate.type.basic.BasicTypeFactory to handle this based on its registry
//
// - or for 6.0 make TemporalType part of the state for those BasicType impls dealing with date/time types
//
// - or for 6.0 make TemporalType part of Binder contract
//
// - or add a org.hibernate.type.TemporalType#getVariant(TemporalType)
//
// - or ...
// todo : (5.2) review Java type handling for sanity. This part was done quickly ;)
final Class javaType;
// Determine the "value java type" :
// prefer to leverage the bindValue java type (if bindValue not null),
// followed by the java type reported by the baseType,
// fallback to java.sql.Timestamp
if ( bindValue != null ) {
javaType = bindValue.getClass();
}
else if ( baseType != null ) {
javaType = baseType.getReturnedClass();
}
else {
javaType = java.sql.Timestamp.class;
}
switch ( temporalType ) {
case TIMESTAMP: {
return resolveTimestampTemporalTypeVariant( javaType, baseType );
}
case DATE: {
return resolveDateTemporalTypeVariant( javaType, baseType );
}
case TIME: {
return resolveTimeTemporalTypeVariant( javaType, baseType );
}
default: {
throw new IllegalArgumentException( "Unexpected TemporalType [" + temporalType + "]; expecting TIMESTAMP, DATE or TIME" );
}
}
}
public ValueImpl(ExportableColumn column, Table table, BasicType type, Database database) {
this.column = column;
this.table = table;
this.type = type;
this.database = database;
}
@Override
public BasicType basic(String name) {
return typeResolver.basic( name );
}
@LogMessage(level = WARN)
@Message(value = "Type [%s] defined no registration keys; ignoring", id = 269)
void typeDefinedNoRegistrationKeys(BasicType type);
@Override
public MetadataBuilder applyBasicType(BasicType type) {
delegate.applyBasicType( type );
return getThis();
}
@Override
public MetadataBuilder applyBasicType(BasicType type, String... keys) {
delegate.applyBasicType( type, keys );
return getThis();
}
public BasicTypeRegistration(BasicType basicType) {
this( basicType, basicType.getRegistrationKeys() );
}
public BasicTypeRegistration(BasicType basicType, String[] registrationKeys) {
this.basicType = basicType;
this.registrationKeys = registrationKeys;
}
public BasicType getBasicType() {
return basicType;
}
@Override
public MetadataBuilder applyBasicType(BasicType type) {
options.basicTypeRegistrations.add( new BasicTypeRegistration( type ) );
return this;
}
@Override
public MetadataBuilder applyBasicType(BasicType type, String... keys) {
options.basicTypeRegistrations.add( new BasicTypeRegistration( type, keys ) );
return this;
}
@Override
public void contributeType(BasicType type) {
options.basicTypeRegistrations.add( new BasicTypeRegistration( type ) );
}
@Override
public void contributeType(BasicType type, String... keys) {
options.basicTypeRegistrations.add( new BasicTypeRegistration( type, keys ) );
}
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();
}
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()
);
}
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()
);
}
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()
);
}
protected String render(BasicType basic, List<String> columns, SessionFactory sessionFactory, SQLFunction sqlFunction) {
return sqlFunction.render(basic, columns, (SessionFactoryImplementor) sessionFactory);
}
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();
}