Search in sources :

Example 6 with IdentifierGenerator

use of org.hibernate.id.IdentifierGenerator in project hibernate-orm by hibernate.

the class Component method buildIdentifierGenerator.

private IdentifierGenerator buildIdentifierGenerator(IdentifierGeneratorFactory identifierGeneratorFactory, Dialect dialect, String defaultCatalog, String defaultSchema, RootClass rootClass) throws MappingException {
    final boolean hasCustomGenerator = !DEFAULT_ID_GEN_STRATEGY.equals(getIdentifierGeneratorStrategy());
    if (hasCustomGenerator) {
        return super.createIdentifierGenerator(identifierGeneratorFactory, dialect, defaultCatalog, defaultSchema, rootClass);
    }
    final Class entityClass = rootClass.getMappedClass();
    // what class is the declarer of the composite pk attributes
    final Class attributeDeclarer;
    CompositeNestedGeneratedValueGenerator.GenerationContextLocator locator;
    //		various scenarios for which we need to account here
    if (rootClass.getIdentifierMapper() != null) {
        // we have the @IdClass / <composite-id mapped="true"/> case
        attributeDeclarer = resolveComponentClass();
    } else if (rootClass.getIdentifierProperty() != null) {
        // we have the "@EmbeddedId" / <composite-id name="idName"/> case
        attributeDeclarer = resolveComponentClass();
    } else {
        // we have the "straight up" embedded (again the hibernate term) component identifier
        attributeDeclarer = entityClass;
    }
    locator = new StandardGenerationContextLocator(rootClass.getEntityName());
    final CompositeNestedGeneratedValueGenerator generator = new CompositeNestedGeneratedValueGenerator(locator);
    Iterator itr = getPropertyIterator();
    while (itr.hasNext()) {
        final Property property = (Property) itr.next();
        if (property.getValue().isSimpleValue()) {
            final SimpleValue value = (SimpleValue) property.getValue();
            if (DEFAULT_ID_GEN_STRATEGY.equals(value.getIdentifierGeneratorStrategy())) {
                // the StandardGenerationContextLocator
                continue;
            }
            final IdentifierGenerator valueGenerator = value.createIdentifierGenerator(identifierGeneratorFactory, dialect, defaultCatalog, defaultSchema, rootClass);
            generator.addGeneratedValuePlan(new ValueGenerationPlan(valueGenerator, injector(property, attributeDeclarer)));
        }
    }
    return generator;
}
Also used : CompositeNestedGeneratedValueGenerator(org.hibernate.id.CompositeNestedGeneratedValueGenerator) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Iterator(java.util.Iterator) IdentifierGenerator(org.hibernate.id.IdentifierGenerator)

Example 7 with IdentifierGenerator

use of org.hibernate.id.IdentifierGenerator in project hibernate-orm by hibernate.

the class NewGeneratorMappingsTest method testMinimalTableEntity.

@Test
public void testMinimalTableEntity() {
    final EntityPersister persister = sessionFactory().getEntityPersister(MinimalTableEntity.class.getName());
    IdentifierGenerator generator = persister.getIdentifierGenerator();
    assertTrue(TableGenerator.class.isInstance(generator));
    TableGenerator tabGenerator = (TableGenerator) generator;
    assertEquals(MinimalTableEntity.TBL_NAME, tabGenerator.getTableName());
    assertEquals(TableGenerator.DEF_SEGMENT_COLUMN, tabGenerator.getSegmentColumnName());
    assertEquals("MINIMAL_TBL", tabGenerator.getSegmentValue());
    assertEquals(TableGenerator.DEF_VALUE_COLUMN, tabGenerator.getValueColumnName());
    // 0 is the annotation default, but its expected to be treated as 1
    assertEquals(1, tabGenerator.getInitialValue());
    // 50 is the annotation default
    assertEquals(50, tabGenerator.getIncrementSize());
    assertTrue(PooledOptimizer.class.isInstance(tabGenerator.getOptimizer()));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) PooledOptimizer(org.hibernate.id.enhanced.PooledOptimizer) TableGenerator(org.hibernate.id.enhanced.TableGenerator) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) Test(org.junit.Test)

Example 8 with IdentifierGenerator

use of org.hibernate.id.IdentifierGenerator in project hibernate-orm by hibernate.

the class NewGeneratorMappingsTest method testSequencePerEntity.

@Test
@TestForIssue(jiraKey = "HHH-6790")
public void testSequencePerEntity() {
    // Checking first entity.
    EntityPersister persister = sessionFactory().getEntityPersister(DedicatedSequenceEntity1.class.getName());
    IdentifierGenerator generator = persister.getIdentifierGenerator();
    assertTrue(SequenceStyleGenerator.class.isInstance(generator));
    SequenceStyleGenerator seqGenerator = (SequenceStyleGenerator) generator;
    assertEquals(StringHelper.unqualifyEntityName(DedicatedSequenceEntity1.class.getName()) + DedicatedSequenceEntity1.SEQUENCE_SUFFIX, seqGenerator.getDatabaseStructure().getName());
    // Checking second entity.
    persister = sessionFactory().getEntityPersister(DedicatedSequenceEntity2.class.getName());
    generator = persister.getIdentifierGenerator();
    assertTrue(SequenceStyleGenerator.class.isInstance(generator));
    seqGenerator = (SequenceStyleGenerator) generator;
    assertEquals(DedicatedSequenceEntity2.ENTITY_NAME + DedicatedSequenceEntity1.SEQUENCE_SUFFIX, seqGenerator.getDatabaseStructure().getName());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SequenceStyleGenerator(org.hibernate.id.enhanced.SequenceStyleGenerator) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 9 with IdentifierGenerator

use of org.hibernate.id.IdentifierGenerator in project hibernate-orm by hibernate.

the class NewGeneratorMappingsTest method testMinimalSequenceEntity.

@Test
public void testMinimalSequenceEntity() {
    final EntityPersister persister = sessionFactory().getEntityPersister(MinimalSequenceEntity.class.getName());
    IdentifierGenerator generator = persister.getIdentifierGenerator();
    assertTrue(SequenceStyleGenerator.class.isInstance(generator));
    SequenceStyleGenerator seqGenerator = (SequenceStyleGenerator) generator;
    assertEquals(MinimalSequenceEntity.SEQ_NAME, seqGenerator.getDatabaseStructure().getName());
    // 1 is the annotation default
    assertEquals(1, seqGenerator.getDatabaseStructure().getInitialValue());
    // 50 is the annotation default
    assertEquals(50, seqGenerator.getDatabaseStructure().getIncrementSize());
    assertFalse(NoopOptimizer.class.isInstance(seqGenerator.getOptimizer()));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NoopOptimizer(org.hibernate.id.enhanced.NoopOptimizer) SequenceStyleGenerator(org.hibernate.id.enhanced.SequenceStyleGenerator) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) Test(org.junit.Test)

Example 10 with IdentifierGenerator

use of org.hibernate.id.IdentifierGenerator in project hibernate-orm by hibernate.

the class NewGeneratorMappingsTest method testAutoEntity.

@Test
public void testAutoEntity() {
    final EntityPersister persister = sessionFactory().getEntityPersister(AutoEntity.class.getName());
    IdentifierGenerator generator = persister.getIdentifierGenerator();
    assertTrue(SequenceStyleGenerator.class.isInstance(generator));
    SequenceStyleGenerator seqGenerator = (SequenceStyleGenerator) generator;
    assertEquals(SequenceStyleGenerator.DEF_SEQUENCE_NAME, seqGenerator.getDatabaseStructure().getName());
    assertEquals(SequenceStyleGenerator.DEFAULT_INITIAL_VALUE, seqGenerator.getDatabaseStructure().getInitialValue());
    assertEquals(SequenceStyleGenerator.DEFAULT_INCREMENT_SIZE, seqGenerator.getDatabaseStructure().getIncrementSize());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SequenceStyleGenerator(org.hibernate.id.enhanced.SequenceStyleGenerator) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) Test(org.junit.Test)

Aggregations

IdentifierGenerator (org.hibernate.id.IdentifierGenerator)12 EntityPersister (org.hibernate.persister.entity.EntityPersister)8 Test (org.junit.Test)8 SequenceStyleGenerator (org.hibernate.id.enhanced.SequenceStyleGenerator)5 BulkInsertionCapableIdentifierGenerator (org.hibernate.id.BulkInsertionCapableIdentifierGenerator)2 NoopOptimizer (org.hibernate.id.enhanced.NoopOptimizer)2 AST (antlr.collections.AST)1 Iterator (java.util.Iterator)1 MappingException (org.hibernate.MappingException)1 QueryException (org.hibernate.QueryException)1 Session (org.hibernate.Session)1 SessionFactory (org.hibernate.SessionFactory)1 Metadata (org.hibernate.boot.Metadata)1 MetadataSources (org.hibernate.boot.MetadataSources)1 MetadataImpl (org.hibernate.boot.internal.MetadataImpl)1 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)1 OrderedSequenceGenerator (org.hibernate.envers.enhanced.OrderedSequenceGenerator)1 SequenceIdRevisionEntity (org.hibernate.envers.enhanced.SequenceIdRevisionEntity)1