Search in sources :

Example 16 with SequenceStyleGenerator

use of org.hibernate.id.enhanced.SequenceStyleGenerator in project hibernate-orm by hibernate.

the class BasicForcedTableSequenceTest method testNormalBoundary.

@Test
public void testNormalBoundary() {
    EntityPersister persister = sessionFactory().getEntityPersister(Entity.class.getName());
    assertTrue("sequence style generator was not used", SequenceStyleGenerator.class.isInstance(persister.getIdentifierGenerator()));
    SequenceStyleGenerator generator = (SequenceStyleGenerator) persister.getIdentifierGenerator();
    assertTrue("table structure was not used", TableStructure.class.isInstance(generator.getDatabaseStructure()));
    assertTrue("no-op optimizer was not used", NoopOptimizer.class.isInstance(generator.getOptimizer()));
    int count = 5;
    Entity[] entities = new Entity[count];
    Session s = openSession();
    s.beginTransaction();
    for (int i = 0; i < count; i++) {
        entities[i] = new Entity("" + (i + 1));
        s.save(entities[i]);
        long expectedId = i + 1;
        assertEquals(expectedId, entities[i].getId().longValue());
        assertEquals(expectedId, generator.getDatabaseStructure().getTimesAccessed());
        assertEquals(expectedId, ((BasicHolder) generator.getOptimizer().getLastSourceValue()).getActualLongValue());
    }
    s.getTransaction().commit();
    s.beginTransaction();
    for (int i = 0; i < count; i++) {
        assertEquals(i + 1, entities[i].getId().intValue());
        s.delete(entities[i]);
    }
    s.getTransaction().commit();
    s.close();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NoopOptimizer(org.hibernate.id.enhanced.NoopOptimizer) TableStructure(org.hibernate.id.enhanced.TableStructure) SequenceStyleGenerator(org.hibernate.id.enhanced.SequenceStyleGenerator) Session(org.hibernate.Session) Test(org.junit.Test)

Example 17 with SequenceStyleGenerator

use of org.hibernate.id.enhanced.SequenceStyleGenerator in project hibernate-orm by hibernate.

the class BasicSequenceTest method testNormalBoundary.

@Test
public void testNormalBoundary() {
    EntityPersister persister = sessionFactory().getEntityPersister(Entity.class.getName());
    assertClassAssignability(SequenceStyleGenerator.class, persister.getIdentifierGenerator().getClass());
    SequenceStyleGenerator generator = (SequenceStyleGenerator) persister.getIdentifierGenerator();
    int count = 5;
    Entity[] entities = new Entity[count];
    Session s = openSession();
    s.beginTransaction();
    for (int i = 0; i < count; i++) {
        entities[i] = new Entity("" + (i + 1));
        s.save(entities[i]);
        long expectedId = i + 1;
        assertEquals(expectedId, entities[i].getId().longValue());
        assertEquals(expectedId, generator.getDatabaseStructure().getTimesAccessed());
        assertEquals(expectedId, ((BasicHolder) generator.getOptimizer().getLastSourceValue()).getActualLongValue());
    }
    s.getTransaction().commit();
    s.beginTransaction();
    for (int i = 0; i < count; i++) {
        assertEquals(i + 1, entities[i].getId().intValue());
        s.delete(entities[i]);
    }
    s.getTransaction().commit();
    s.close();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SequenceStyleGenerator(org.hibernate.id.enhanced.SequenceStyleGenerator) Session(org.hibernate.Session) Test(org.junit.Test)

Example 18 with SequenceStyleGenerator

use of org.hibernate.id.enhanced.SequenceStyleGenerator in project hibernate-orm by hibernate.

the class GeneratedValueTests method testExplicitSequenceGeneratorImplicitNamePreferGeneratorName.

@Test
public void testExplicitSequenceGeneratorImplicitNamePreferGeneratorName() {
    // this should be the default behavior
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    final Metadata bootModel = new MetadataSources(ssr).addAnnotatedClass(ExplicitSequenceGeneratorImplicitNameEntity.class).buildMetadata();
    final PersistentClass entityMapping = bootModel.getEntityBinding(ExplicitSequenceGeneratorImplicitNameEntity.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);
    // all the JPA defaults since they were not defined
    assertThat(sequenceStyleGenerator.getDatabaseStructure().getName(), is("my_db_sequence"));
    assertThat(sequenceStyleGenerator.getDatabaseStructure().getInitialValue(), is(100));
    assertThat(sequenceStyleGenerator.getDatabaseStructure().getIncrementSize(), is(500));
    final Sequence sequence = bootModel.getDatabase().getDefaultNamespace().locateSequence(Identifier.toIdentifier("my_db_sequence"));
    assertThat(sequence, notNullValue());
    final String[] sqlCreateStrings = new H2Dialect().getSequenceExporter().getSqlCreateStrings(sequence, bootModel);
    assertThat(sqlCreateStrings.length, is(1));
    final String cmd = sqlCreateStrings[0].toLowerCase();
    assertTrue(cmd.startsWith("create sequence my_db_sequence start with 100 increment by 500"));
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) H2Dialect(org.hibernate.dialect.H2Dialect) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) SequenceStyleGenerator(org.hibernate.id.enhanced.SequenceStyleGenerator) Sequence(org.hibernate.boot.model.relational.Sequence) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) Test(org.junit.Test)

Example 19 with SequenceStyleGenerator

use of org.hibernate.id.enhanced.SequenceStyleGenerator in project hibernate-orm by hibernate.

the class GeneratedValueTests method testImplicitSequenceGeneratorGeneratorName.

@Test
public void testImplicitSequenceGeneratorGeneratorName() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    final Metadata bootModel = new MetadataSources(ssr).addAnnotatedClass(ImplicitSequenceGeneratorEntity.class).buildMetadata();
    final PersistentClass entityMapping = bootModel.getEntityBinding(ImplicitSequenceGeneratorEntity.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);
    // PREFER_GENERATOR_NAME_AS_DEFAULT_SEQUENCE_NAME == true (the default) indicates that the generator-name
    // should be used as the default instead.
    assertThat(sequenceStyleGenerator.getDatabaseStructure().getName(), is("my_db_sequence"));
    // the JPA defaults since they were not defined
    assertThat(sequenceStyleGenerator.getDatabaseStructure().getInitialValue(), is(1));
    assertThat(sequenceStyleGenerator.getDatabaseStructure().getIncrementSize(), is(50));
}
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)

Example 20 with SequenceStyleGenerator

use of org.hibernate.id.enhanced.SequenceStyleGenerator in project hibernate-orm by hibernate.

the class NegativeValueSequenceTest method assertOptimizer.

private void assertOptimizer(SessionFactoryImplementor sessionFactory, Class<?> entityClass, Class<? extends Optimizer> expectedOptimizerClass) {
    assertTrue(SequenceStyleGenerator.class.isInstance(sessionFactory.getMetamodel().entityPersister(entityClass).getIdentifierGenerator()));
    SequenceStyleGenerator generator = (SequenceStyleGenerator) sessionFactory.getMetamodel().entityPersister(entityClass).getIdentifierGenerator();
    assertTrue(expectedOptimizerClass.isInstance(generator.getOptimizer()));
}
Also used : SequenceStyleGenerator(org.hibernate.id.enhanced.SequenceStyleGenerator)

Aggregations

SequenceStyleGenerator (org.hibernate.id.enhanced.SequenceStyleGenerator)20 Test (org.junit.Test)18 EntityPersister (org.hibernate.persister.entity.EntityPersister)12 IdentifierGenerator (org.hibernate.id.IdentifierGenerator)10 Session (org.hibernate.Session)7 Metadata (org.hibernate.boot.Metadata)6 MetadataSources (org.hibernate.boot.MetadataSources)6 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)6 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)5 PersistentClass (org.hibernate.mapping.PersistentClass)5 NoopOptimizer (org.hibernate.id.enhanced.NoopOptimizer)3 TableStructure (org.hibernate.id.enhanced.TableStructure)3 Properties (java.util.Properties)2 HiLoOptimizer (org.hibernate.id.enhanced.HiLoOptimizer)2 PooledOptimizer (org.hibernate.id.enhanced.PooledOptimizer)2 TestForIssue (org.hibernate.testing.TestForIssue)2 ATest (de.invesdwin.context.test.ATest)1 ObjectNameNormalizer (org.hibernate.boot.model.naming.ObjectNameNormalizer)1 Sequence (org.hibernate.boot.model.relational.Sequence)1 MetadataBuildingContext (org.hibernate.boot.spi.MetadataBuildingContext)1