Search in sources :

Example 6 with TypeFactory

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

the class TypeFactorySerializationTest method testWithSameRegisteredSessionFactory.

@Test
public void testWithSameRegisteredSessionFactory() throws Exception {
    Configuration cfg = new Configuration().setProperty(AvailableSettings.SESSION_FACTORY_NAME, NAME).setProperty(AvailableSettings.SESSION_FACTORY_NAME_IS_JNDI, // default is true
    "false");
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory();
    // Session factory is registered.
    assertSame(factory, SessionFactoryRegistry.INSTANCE.getNamedSessionFactory(NAME));
    TypeFactory typeFactory = factory.getTypeResolver().getTypeFactory();
    byte[] typeFactoryBytes = SerializationHelper.serialize(typeFactory);
    typeFactory = (TypeFactory) SerializationHelper.deserialize(typeFactoryBytes);
    assertSame(factory, typeFactory.resolveSessionFactory());
    factory.close();
}
Also used : Configuration(org.hibernate.cfg.Configuration) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) TypeFactory(org.hibernate.type.TypeFactory) Test(org.junit.Test)

Example 7 with TypeFactory

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

the class TypeFactorySerializationTest method testUnregisterSerializeRegisterSameSessionFactory.

@Test
public void testUnregisterSerializeRegisterSameSessionFactory() throws Exception {
    Configuration cfg = new Configuration().setProperty(AvailableSettings.SESSION_FACTORY_NAME, NAME).setProperty(AvailableSettings.SESSION_FACTORY_NAME_IS_JNDI, // default is true
    "false");
    SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory();
    assertSame(factory, SessionFactoryRegistry.INSTANCE.getNamedSessionFactory(NAME));
    // Remove the session factory from the registry
    SessionFactoryRegistry.INSTANCE.removeSessionFactory(factory.getUuid(), NAME, false, null);
    assertNull(SessionFactoryRegistry.INSTANCE.findSessionFactory(factory.getUuid(), NAME));
    TypeFactory typeFactory = factory.getTypeResolver().getTypeFactory();
    byte[] typeFactoryBytes = SerializationHelper.serialize(typeFactory);
    typeFactory = (TypeFactory) SerializationHelper.deserialize(typeFactoryBytes);
    try {
        typeFactory.resolveSessionFactory();
        fail("should have failed with HibernateException because session factory is not registered.");
    } catch (HibernateException ex) {
    // expected because the session factory is not registered.
    }
    // Re-register the same session factory.
    SessionFactoryRegistry.INSTANCE.addSessionFactory(factory.getUuid(), NAME, false, factory, null);
    // Session factory resolved from typeFactory should be the new session factory
    // (because it is resolved from SessionFactoryRegistry.INSTANCE)
    assertSame(factory, typeFactory.resolveSessionFactory());
    factory.close();
}
Also used : Configuration(org.hibernate.cfg.Configuration) HibernateException(org.hibernate.HibernateException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) TypeFactory(org.hibernate.type.TypeFactory) Test(org.junit.Test)

Aggregations

TypeFactory (org.hibernate.type.TypeFactory)7 Configuration (org.hibernate.cfg.Configuration)5 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)5 Test (org.junit.Test)5 HibernateException (org.hibernate.HibernateException)4 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ClassLoaderAccessImpl (org.hibernate.boot.internal.ClassLoaderAccessImpl)1 InFlightMetadataCollectorImpl (org.hibernate.boot.internal.InFlightMetadataCollectorImpl)1 MetadataBuildingContextRootImpl (org.hibernate.boot.internal.MetadataBuildingContextRootImpl)1 MappingBinder (org.hibernate.boot.jaxb.internal.MappingBinder)1 AnnotationMetadataSourceProcessorImpl (org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl)1 EntityHierarchyBuilder (org.hibernate.boot.model.source.internal.hbm.EntityHierarchyBuilder)1 EntityHierarchySourceImpl (org.hibernate.boot.model.source.internal.hbm.EntityHierarchySourceImpl)1 HbmMetadataSourceProcessorImpl (org.hibernate.boot.model.source.internal.hbm.HbmMetadataSourceProcessorImpl)1 MappingDocument (org.hibernate.boot.model.source.internal.hbm.MappingDocument)1 ModelBinder (org.hibernate.boot.model.source.internal.hbm.ModelBinder)1 MetadataSourceProcessor (org.hibernate.boot.model.source.spi.MetadataSourceProcessor)1 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)1 AdditionalJaxbMappingProducer (org.hibernate.boot.spi.AdditionalJaxbMappingProducer)1