HHH-12443 - Introduce TypeConfiguration

added StandardBasicTypeTemplate
This commit is contained in:
Steve Ebersole 2018-03-28 21:05:48 -05:00
parent b228a2bc83
commit ed5afc0877
4 changed files with 109 additions and 24 deletions

View File

@ -252,7 +252,7 @@ public abstract class EntityType extends AbstractType implements AssociationType
return ReflectHelper.classForName( entityName );
}
catch (ClassNotFoundException cnfe) {
return this.scope.getTypeConfiguration().resolveSessionFactory().getMetamodel().entityPersister( entityName ).
return this.scope.getTypeConfiguration().getSessionFactory().getMetamodel().entityPersister( entityName ).
getEntityTuplizer().getMappedClass();
}
}

View File

@ -0,0 +1,44 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.type;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
/**
* A BasicType adapter targeting partial portability to 6.0's type
* system changes. In 6.0 the notion of a BasicType is just a
* combination of JavaTypeDescriptor/SqlTypeDescriptor.
*
* @author Steve Ebersole
*/
@SuppressWarnings("unused")
public class StandardBasicTypeTemplate<J> extends AbstractSingleColumnStandardBasicType<J> {
private final String name;
private final String[] registrationKeys;
public StandardBasicTypeTemplate(
SqlTypeDescriptor sqlTypeDescriptor,
JavaTypeDescriptor<J> javaTypeDescriptor,
String... registrationKeys) {
super( sqlTypeDescriptor, javaTypeDescriptor );
this.registrationKeys = registrationKeys;
this.name = javaTypeDescriptor.getJavaType() == null ? "(map-mode)" : javaTypeDescriptor.getJavaType().getName()
+ " -> " + sqlTypeDescriptor.getSqlType();
}
@Override
public String getName() {
return name;
}
@Override
public String[] getRegistrationKeys() {
return registrationKeys;
}
}

View File

@ -198,10 +198,6 @@ public class TypeConfiguration implements SessionFactoryObserver, Serializable {
// valid while the TypeConfiguration is scoped to SessionFactory
}
public SessionFactoryImplementor resolveSessionFactory() {
return scope.resolveSessionFactory();
}
/**
* Encapsulation of lifecycle concerns for a TypeConfiguration, mainly in
* regards to eventually being associated with a SessionFactory. Goes
@ -317,25 +313,6 @@ public class TypeConfiguration implements SessionFactoryObserver, Serializable {
return this;
}
public SessionFactoryImplementor resolveSessionFactory() {
// if ( sessionFactory == null ) {
// if ( sessionFactoryName != null || sessionFactoryUuid != null ) {
// sessionFactory = (SessionFactoryImplementor) SessionFactoryRegistry.INSTANCE.findSessionFactory(
// sessionFactoryUuid,
// sessionFactoryName
// );
//
// if ( sessionFactory == null ) {
// throw new HibernateException(
// "Could not find a SessionFactory [uuid=" + sessionFactoryUuid + ",name=" + sessionFactoryName + "]"
// );
// }
// }
// }
return sessionFactory;
}
}

View File

@ -0,0 +1,64 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.test.type;
import java.net.URL;
import org.hibernate.type.BasicType;
import org.hibernate.type.StandardBasicTypeTemplate;
import org.hibernate.type.descriptor.java.UrlTypeDescriptor;
import org.hibernate.type.descriptor.sql.VarcharTypeDescriptor;
import org.hibernate.type.spi.TypeConfiguration;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
/**
* Test making sure StandardBasicTypeTemplate works
*
* @author Steve Ebersole
*/
public class StandardBasicTypeTemplateTest extends BaseUnitTestCase {
public static final String REG_KEY = "validating-url";
@Test
public void testContributedBasicType() {
TypeConfiguration typeConfiguration = new TypeConfiguration();
typeConfiguration.getJavaTypeDescriptorRegistry().addDescriptor( ValidatingUrlJavaTypeDescriptor.INSTANCE );
typeConfiguration.getBasicTypeRegistry().register(
new StandardBasicTypeTemplate<>(
VarcharTypeDescriptor.INSTANCE,
ValidatingUrlJavaTypeDescriptor.INSTANCE,
REG_KEY
)
);
final BasicType registeredType = typeConfiguration.getBasicTypeRegistry().getRegisteredType( REG_KEY );
assertThat( registeredType, notNullValue() );
assertTyping( StandardBasicTypeTemplate.class, registeredType );
}
private static class ValidatingUrlJavaTypeDescriptor extends UrlTypeDescriptor {
/**
* Singleton access
*/
public static final ValidatingUrlJavaTypeDescriptor INSTANCE = new ValidatingUrlJavaTypeDescriptor();
@Override
public URL fromString(String string) {
if ( "invalid".equals( string ) ) {
throw new IllegalStateException( "Invalid url" );
}
return super.fromString( string );
}
}
}