use of org.hibernate.type.TypeFactory in project hibernate-orm by hibernate.
the class TypeFactorySerializationTest method testUnregisterSerializeRegisterDiffSessionFactory.
@Test
public void testUnregisterSerializeRegisterDiffSessionFactory() 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.
}
// Now create a new session factory with the same name; it will have a different UUID.
SessionFactoryImplementor factoryWithSameName = (SessionFactoryImplementor) cfg.buildSessionFactory();
assertSame(factoryWithSameName, SessionFactoryRegistry.INSTANCE.getNamedSessionFactory(NAME));
assertFalse(factory.getUuid().equals(factoryWithSameName.getUuid()));
// Session factory resolved from typeFactory should be the new session factory
// (because it is resolved from SessionFactoryRegistry.INSTANCE)
assertSame(factoryWithSameName, typeFactory.resolveSessionFactory());
factory.close();
factoryWithSameName.close();
}
use of org.hibernate.type.TypeFactory in project hibernate-orm by hibernate.
the class TypeFactorySerializationTest method testUnregisterSerializeRegisterDiffSessionFactoryNoName.
@Test
public void testUnregisterSerializeRegisterDiffSessionFactoryNoName() throws Exception {
Configuration cfg = new Configuration();
SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory();
assertSame(factory, SessionFactoryRegistry.INSTANCE.getSessionFactory(factory.getUuid()));
// Remove the session factory from the registry
SessionFactoryRegistry.INSTANCE.removeSessionFactory(factory.getUuid(), null, false, null);
assertNull(SessionFactoryRegistry.INSTANCE.getSessionFactory(factory.getUuid()));
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.
}
// Now create a new session factory with the same name; it will have a different UUID.
SessionFactoryImplementor factoryWithDiffUuid = (SessionFactoryImplementor) cfg.buildSessionFactory();
assertSame(factoryWithDiffUuid, SessionFactoryRegistry.INSTANCE.getSessionFactory(factoryWithDiffUuid.getUuid()));
assertFalse(factory.getUuid().equals(factoryWithDiffUuid.getUuid()));
// It should not be possible to resolve the session factory with no name configured.
try {
typeFactory.resolveSessionFactory();
fail("should have failed with HibernateException because session factories were not registered with the same non-null name.");
} catch (HibernateException ex) {
// expected
}
factory.close();
factoryWithDiffUuid.close();
}
use of org.hibernate.type.TypeFactory in project hibernate-orm by hibernate.
the class MetadataBuildingProcess method complete.
/**
* Second step of 2-phase for MetadataSources->Metadata process
*
* @param managedResources The token/memento from 1st phase
* @param options The building options
*
* @return Token/memento representing all known users resources (classes, packages, mapping files, etc).
*/
public static MetadataImplementor complete(final ManagedResources managedResources, final MetadataBuildingOptions options) {
final BasicTypeRegistry basicTypeRegistry = handleTypes(options);
final InFlightMetadataCollectorImpl metadataCollector = new InFlightMetadataCollectorImpl(options, new TypeResolver(basicTypeRegistry, new TypeFactory()));
for (AttributeConverterDefinition attributeConverterDefinition : managedResources.getAttributeConverterDefinitions()) {
metadataCollector.addAttributeConverter(attributeConverterDefinition);
}
final ClassLoaderService classLoaderService = options.getServiceRegistry().getService(ClassLoaderService.class);
final ClassLoaderAccess classLoaderAccess = new ClassLoaderAccessImpl(options.getTempClassLoader(), classLoaderService);
final MetadataBuildingContextRootImpl rootMetadataBuildingContext = new MetadataBuildingContextRootImpl(options, classLoaderAccess, metadataCollector);
final IndexView jandexView = options.getJandexView();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Set up the processors and start binding
// NOTE : this becomes even more simplified afterQuery we move purely
// to unified model
final MetadataSourceProcessor processor = new MetadataSourceProcessor() {
private final HbmMetadataSourceProcessorImpl hbmProcessor = new HbmMetadataSourceProcessorImpl(managedResources, rootMetadataBuildingContext);
private final AnnotationMetadataSourceProcessorImpl annotationProcessor = new AnnotationMetadataSourceProcessorImpl(managedResources, rootMetadataBuildingContext, jandexView);
@Override
public void prepare() {
hbmProcessor.prepare();
annotationProcessor.prepare();
}
@Override
public void processTypeDefinitions() {
hbmProcessor.processTypeDefinitions();
annotationProcessor.processTypeDefinitions();
}
@Override
public void processQueryRenames() {
hbmProcessor.processQueryRenames();
annotationProcessor.processQueryRenames();
}
@Override
public void processNamedQueries() {
hbmProcessor.processNamedQueries();
annotationProcessor.processNamedQueries();
}
@Override
public void processAuxiliaryDatabaseObjectDefinitions() {
hbmProcessor.processAuxiliaryDatabaseObjectDefinitions();
annotationProcessor.processAuxiliaryDatabaseObjectDefinitions();
}
@Override
public void processIdentifierGenerators() {
hbmProcessor.processIdentifierGenerators();
annotationProcessor.processIdentifierGenerators();
}
@Override
public void processFilterDefinitions() {
hbmProcessor.processFilterDefinitions();
annotationProcessor.processFilterDefinitions();
}
@Override
public void processFetchProfiles() {
hbmProcessor.processFetchProfiles();
annotationProcessor.processFetchProfiles();
}
@Override
public void prepareForEntityHierarchyProcessing() {
for (MetadataSourceType metadataSourceType : options.getSourceProcessOrdering()) {
if (metadataSourceType == MetadataSourceType.HBM) {
hbmProcessor.prepareForEntityHierarchyProcessing();
}
if (metadataSourceType == MetadataSourceType.CLASS) {
annotationProcessor.prepareForEntityHierarchyProcessing();
}
}
}
@Override
public void processEntityHierarchies(Set<String> processedEntityNames) {
for (MetadataSourceType metadataSourceType : options.getSourceProcessOrdering()) {
if (metadataSourceType == MetadataSourceType.HBM) {
hbmProcessor.processEntityHierarchies(processedEntityNames);
}
if (metadataSourceType == MetadataSourceType.CLASS) {
annotationProcessor.processEntityHierarchies(processedEntityNames);
}
}
}
@Override
public void postProcessEntityHierarchies() {
for (MetadataSourceType metadataSourceType : options.getSourceProcessOrdering()) {
if (metadataSourceType == MetadataSourceType.HBM) {
hbmProcessor.postProcessEntityHierarchies();
}
if (metadataSourceType == MetadataSourceType.CLASS) {
annotationProcessor.postProcessEntityHierarchies();
}
}
}
@Override
public void processResultSetMappings() {
hbmProcessor.processResultSetMappings();
annotationProcessor.processResultSetMappings();
}
@Override
public void finishUp() {
hbmProcessor.finishUp();
annotationProcessor.finishUp();
}
};
processor.prepare();
processor.processTypeDefinitions();
processor.processQueryRenames();
processor.processAuxiliaryDatabaseObjectDefinitions();
processor.processIdentifierGenerators();
processor.processFilterDefinitions();
processor.processFetchProfiles();
final Set<String> processedEntityNames = new HashSet<String>();
processor.prepareForEntityHierarchyProcessing();
processor.processEntityHierarchies(processedEntityNames);
processor.postProcessEntityHierarchies();
processor.processResultSetMappings();
processor.processNamedQueries();
processor.finishUp();
for (MetadataContributor contributor : classLoaderService.loadJavaServices(MetadataContributor.class)) {
log.tracef("Calling MetadataContributor : %s", contributor);
contributor.contribute(metadataCollector, jandexView);
}
metadataCollector.processSecondPasses(rootMetadataBuildingContext);
Iterable<AdditionalJaxbMappingProducer> producers = classLoaderService.loadJavaServices(AdditionalJaxbMappingProducer.class);
if (producers != null) {
final EntityHierarchyBuilder hierarchyBuilder = new EntityHierarchyBuilder();
// final MappingBinder mappingBinder = new MappingBinder( true );
// We need to disable validation here. It seems Envers is not producing valid (according to schema) XML
final MappingBinder mappingBinder = new MappingBinder(classLoaderService, false);
for (AdditionalJaxbMappingProducer producer : producers) {
log.tracef("Calling AdditionalJaxbMappingProducer : %s", producer);
Collection<MappingDocument> additionalMappings = producer.produceAdditionalMappings(metadataCollector, jandexView, mappingBinder, rootMetadataBuildingContext);
for (MappingDocument mappingDocument : additionalMappings) {
hierarchyBuilder.indexMappingDocument(mappingDocument);
}
}
ModelBinder binder = ModelBinder.prepare(rootMetadataBuildingContext);
for (EntityHierarchySourceImpl entityHierarchySource : hierarchyBuilder.buildHierarchies()) {
binder.bindEntityHierarchy(entityHierarchySource);
}
}
return metadataCollector.buildMetadataInstance(rootMetadataBuildingContext);
}
use of org.hibernate.type.TypeFactory in project hibernate-orm by hibernate.
the class Component method getType.
@Override
public Type getType() throws MappingException {
// TODO : temporary initial step towards HHH-1907
final ComponentMetamodel metamodel = new ComponentMetamodel(this, getMetadata().getMetadataBuildingOptions());
final TypeFactory factory = getMetadata().getTypeResolver().getTypeFactory();
return isEmbedded() ? factory.embeddedComponent(metamodel) : factory.component(metamodel);
}
use of org.hibernate.type.TypeFactory in project hibernate-orm by hibernate.
the class TypeFactorySerializationTest method testUnregisterSerializeRegisterSameSessionFactoryNoName.
@Test
public void testUnregisterSerializeRegisterSameSessionFactoryNoName() throws Exception {
Configuration cfg = new Configuration();
SessionFactoryImplementor factory = (SessionFactoryImplementor) cfg.buildSessionFactory();
assertSame(factory, SessionFactoryRegistry.INSTANCE.findSessionFactory(factory.getUuid(), null));
// Remove the session factory from the registry
SessionFactoryRegistry.INSTANCE.removeSessionFactory(factory.getUuid(), null, false, null);
assertNull(SessionFactoryRegistry.INSTANCE.findSessionFactory(factory.getUuid(), null));
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(), null, 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();
}
Aggregations