Search in sources :

Example 96 with PersistentClass

use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.

the class DiscriminatorMultiTenancyTest method setUp.

@Before
public void setUp() {
    Map settings = new HashMap();
    settings.put(Environment.MULTI_TENANT, MultiTenancyStrategy.DISCRIMINATOR);
    settings.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantResolver);
    settings.put(Environment.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName());
    settings.put(Environment.GENERATE_STATISTICS, "true");
    ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySettings(settings).build();
    MetadataSources ms = new MetadataSources(serviceRegistry);
    ms.addAnnotatedClass(Customer.class);
    Metadata metadata = ms.buildMetadata();
    final PersistentClass customerMapping = metadata.getEntityBinding(Customer.class.getName());
    customerMapping.setCached(true);
    ((RootClass) customerMapping).setCacheConcurrencyStrategy("read-write");
    HibernateSchemaManagementTool tool = new HibernateSchemaManagementTool();
    tool.injectServices(serviceRegistry);
    connectionProvider = ConnectionProviderBuilder.buildConnectionProvider();
    final GenerationTargetToDatabase target = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, connectionProvider));
    new SchemaDropperImpl(serviceRegistry).doDrop(metadata, serviceRegistry, settings, true, target);
    new SchemaCreatorImpl(serviceRegistry).doCreation(metadata, serviceRegistry, settings, true, target);
    target.release();
    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    sessionFactory = (SessionFactoryImplementor) sfb.build();
}
Also used : RootClass(org.hibernate.mapping.RootClass) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) HashMap(java.util.HashMap) Customer(org.hibernate.test.multitenancy.schema.Customer) CachingRegionFactory(org.hibernate.testing.cache.CachingRegionFactory) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) ServiceRegistryImplementor(org.hibernate.service.spi.ServiceRegistryImplementor) HibernateSchemaManagementTool(org.hibernate.tool.schema.internal.HibernateSchemaManagementTool) DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase) HashMap(java.util.HashMap) Map(java.util.Map) PersistentClass(org.hibernate.mapping.PersistentClass) Before(org.junit.Before)

Example 97 with PersistentClass

use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.

the class AbstractSchemaBasedMultiTenancyTest method setUp.

@Before
public void setUp() {
    T multiTenantConnectionProvider = buildMultiTenantConnectionProvider();
    Map settings = new HashMap();
    settings.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA);
    settings.put(Environment.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName());
    settings.put(Environment.GENERATE_STATISTICS, "true");
    serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySettings(settings).addService(MultiTenantConnectionProvider.class, multiTenantConnectionProvider).build();
    MetadataSources ms = new MetadataSources(serviceRegistry);
    ms.addAnnotatedClass(Customer.class);
    ms.addAnnotatedClass(Invoice.class);
    Metadata metadata = ms.buildMetadata();
    final PersistentClass customerMapping = metadata.getEntityBinding(Customer.class.getName());
    customerMapping.setCached(true);
    ((RootClass) customerMapping).setCacheConcurrencyStrategy("read-write");
    HibernateSchemaManagementTool tool = new HibernateSchemaManagementTool();
    tool.injectServices(serviceRegistry);
    final GenerationTargetToDatabase acmeTarget = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, acmeProvider));
    final GenerationTargetToDatabase jbossTarget = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, jbossProvider));
    new SchemaDropperImpl(serviceRegistry).doDrop(metadata, serviceRegistry, settings, true, acmeTarget, jbossTarget);
    new SchemaCreatorImpl(serviceRegistry).doCreation(metadata, serviceRegistry, settings, true, acmeTarget, jbossTarget);
    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    configure(sfb);
    sessionFactory = (SessionFactoryImplementor) sfb.build();
}
Also used : RootClass(org.hibernate.mapping.RootClass) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) HashMap(java.util.HashMap) CachingRegionFactory(org.hibernate.testing.cache.CachingRegionFactory) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) HibernateSchemaManagementTool(org.hibernate.tool.schema.internal.HibernateSchemaManagementTool) DdlTransactionIsolatorTestingImpl(org.hibernate.test.util.DdlTransactionIsolatorTestingImpl) SchemaDropperImpl(org.hibernate.tool.schema.internal.SchemaDropperImpl) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) MultiTenantConnectionProvider(org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider) GenerationTargetToDatabase(org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase) HashMap(java.util.HashMap) Map(java.util.Map) PersistentClass(org.hibernate.mapping.PersistentClass) Before(org.junit.Before)

Example 98 with PersistentClass

use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.

the class VersionsJoinTableRangeComponentNamingTest method testOverrideNotAudited.

/**
 * Verify that
 * {@link VersionsJoinTableRangeComponentTestEntity#getComponent3()} is
 * partially audited.
 */
@Test
public void testOverrideNotAudited() {
    PersistentClass auditClass = metadata().getEntityBinding(VersionsJoinTableRangeComponentTestEntity.class.getName() + "_AUD");
    assert auditClass != null;
    @SuppressWarnings({ "unchecked" }) Iterator<Column> columns = auditClass.getTable().getColumnIterator();
    boolean auditColumn1Found = false;
    boolean auditColumn2Found = false;
    while (columns.hasNext()) {
        Column column = columns.next();
        if ("STR1".equals(column.getName())) {
            auditColumn1Found = true;
        }
        if ("STR2".equals(column.getName())) {
            auditColumn2Found = true;
        }
    }
    assert auditColumn1Found && !auditColumn2Found;
}
Also used : Column(org.hibernate.mapping.Column) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test)

Example 99 with PersistentClass

use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.

the class EntitiesConfigurator method configure.

public EntitiesConfigurations configure(MetadataImplementor metadata, ServiceRegistry serviceRegistry, ReflectionManager reflectionManager, MappingCollector mappingCollector, GlobalConfiguration globalConfiguration, AuditEntitiesConfiguration auditEntitiesConfiguration, AuditStrategy auditStrategy, Document revisionInfoXmlMapping, Element revisionInfoRelationMapping) {
    // Creating a name register to capture all audit entity names created.
    final AuditEntityNameRegister auditEntityNameRegister = new AuditEntityNameRegister();
    // Sorting the persistent class topologically - superclass always before subclass
    final Iterator<PersistentClass> classes = GraphTopologicalSort.sort(new PersistentClassGraphDefiner(metadata)).iterator();
    final ClassesAuditingData classesAuditingData = new ClassesAuditingData();
    final Map<PersistentClass, EntityXmlMappingData> xmlMappings = new HashMap<>();
    // Reading metadata from annotations
    while (classes.hasNext()) {
        final PersistentClass pc = classes.next();
        // Ensure we're in POJO, not dynamic model, mapping.
        if (pc.getClassName() != null) {
            // Collecting information from annotations on the persistent class pc
            final AnnotationsMetadataReader annotationsMetadataReader = new AnnotationsMetadataReader(globalConfiguration, reflectionManager, pc);
            final ClassAuditingData auditData = annotationsMetadataReader.getAuditData();
            classesAuditingData.addClassAuditingData(pc, auditData);
        }
    }
    // Now that all information is read we can update the calculated fields.
    classesAuditingData.updateCalculatedFields();
    final AuditMetadataGenerator auditMetaGen = new AuditMetadataGenerator(metadata, serviceRegistry, globalConfiguration, auditEntitiesConfiguration, auditStrategy, revisionInfoRelationMapping, auditEntityNameRegister);
    // First pass
    for (Map.Entry<PersistentClass, ClassAuditingData> pcDatasEntry : classesAuditingData.getAllClassAuditedData()) {
        final PersistentClass pc = pcDatasEntry.getKey();
        final ClassAuditingData auditData = pcDatasEntry.getValue();
        final EntityXmlMappingData xmlMappingData = new EntityXmlMappingData();
        if (auditData.isAudited()) {
            if (!StringTools.isEmpty(auditData.getAuditTable().value())) {
                auditEntitiesConfiguration.addCustomAuditTableName(pc.getEntityName(), auditData.getAuditTable().value());
            }
            auditMetaGen.generateFirstPass(pc, auditData, xmlMappingData, true);
        } else {
            auditMetaGen.generateFirstPass(pc, auditData, xmlMappingData, false);
        }
        xmlMappings.put(pc, xmlMappingData);
    }
    // Second pass
    for (Map.Entry<PersistentClass, ClassAuditingData> pcDatasEntry : classesAuditingData.getAllClassAuditedData()) {
        final EntityXmlMappingData xmlMappingData = xmlMappings.get(pcDatasEntry.getKey());
        if (pcDatasEntry.getValue().isAudited()) {
            auditMetaGen.generateSecondPass(pcDatasEntry.getKey(), pcDatasEntry.getValue(), xmlMappingData);
            try {
                mappingCollector.addDocument(xmlMappingData.getMainXmlMapping());
                for (Document additionalMapping : xmlMappingData.getAdditionalXmlMappings()) {
                    mappingCollector.addDocument(additionalMapping);
                }
            } catch (DocumentException e) {
                throw new MappingException(e);
            }
        }
    }
    // Only if there are any versioned classes
    if (auditMetaGen.getEntitiesConfigurations().size() > 0) {
        try {
            if (revisionInfoXmlMapping != null) {
                mappingCollector.addDocument(revisionInfoXmlMapping);
            }
        } catch (DocumentException e) {
            throw new MappingException(e);
        }
    }
    return new EntitiesConfigurations(auditMetaGen.getEntitiesConfigurations(), auditMetaGen.getNotAuditedEntitiesConfigurations());
}
Also used : AuditMetadataGenerator(org.hibernate.envers.configuration.internal.metadata.AuditMetadataGenerator) HashMap(java.util.HashMap) EntityXmlMappingData(org.hibernate.envers.configuration.internal.metadata.EntityXmlMappingData) Document(org.dom4j.Document) AuditEntityNameRegister(org.hibernate.envers.configuration.internal.metadata.AuditEntityNameRegister) MappingException(org.hibernate.MappingException) AnnotationsMetadataReader(org.hibernate.envers.configuration.internal.metadata.reader.AnnotationsMetadataReader) ClassAuditingData(org.hibernate.envers.configuration.internal.metadata.reader.ClassAuditingData) DocumentException(org.dom4j.DocumentException) HashMap(java.util.HashMap) Map(java.util.Map) PersistentClass(org.hibernate.mapping.PersistentClass) EntitiesConfigurations(org.hibernate.envers.internal.entities.EntitiesConfigurations)

Example 100 with PersistentClass

use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.

the class GeneratedValueTests method baseline.

@Test
public void baseline() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    final Metadata bootModel = new MetadataSources(ssr).addAnnotatedClass(ExplicitGeneratorEntity.class).buildMetadata();
    final PersistentClass entityMapping = bootModel.getEntityBinding(ExplicitGeneratorEntity.class.getName());
    final IdentifierGenerator generator = entityMapping.getIdentifier().createIdentifierGenerator(bootModel.getIdentifierGeneratorFactory(), ssr.getService(JdbcEnvironment.class).getDialect(), null, null, (RootClass) entityMapping);
    final SequenceStyleGenerator sequenceStyleGenerator = assertTyping(SequenceStyleGenerator.class, generator);
    assertThat(sequenceStyleGenerator.getDatabaseStructure().getName(), is("my_real_db_sequence"));
    // all the JPA defaults since they were not defined
    assertThat(sequenceStyleGenerator.getDatabaseStructure().getInitialValue(), is(100));
    assertThat(sequenceStyleGenerator.getDatabaseStructure().getIncrementSize(), is(500));
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) SequenceStyleGenerator(org.hibernate.id.enhanced.SequenceStyleGenerator) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) Test(org.junit.Test)

Aggregations

PersistentClass (org.hibernate.mapping.PersistentClass)229 Test (org.junit.Test)121 Property (org.hibernate.mapping.Property)65 MetadataSources (org.hibernate.boot.MetadataSources)53 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)52 RootClass (org.hibernate.mapping.RootClass)50 Metadata (org.hibernate.boot.Metadata)45 Column (org.hibernate.mapping.Column)43 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)42 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)38 Iterator (java.util.Iterator)30 SimpleValue (org.hibernate.mapping.SimpleValue)24 HashMap (java.util.HashMap)22 Collection (org.hibernate.mapping.Collection)21 MappingException (org.hibernate.MappingException)20 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)20 Table (org.hibernate.mapping.Table)20 Map (java.util.Map)19 ArrayList (java.util.ArrayList)18 TestForIssue (org.hibernate.testing.TestForIssue)18