Search in sources :

Example 1 with TableGenerator

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

the class Db2GenerationTest method testNewGeneratorTableCreationOnDb2.

@Test
@TestForIssue(jiraKey = "HHH-9850")
public void testNewGeneratorTableCreationOnDb2() {
    StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.DIALECT, DB2Dialect.class.getName()).build();
    try {
        Metadata metadata = new MetadataSources(ssr).buildMetadata();
        assertEquals(0, metadata.getDatabase().getDefaultNamespace().getTables().size());
        TableGenerator generator = new TableGenerator();
        Properties properties = new Properties();
        generator.configure(IntegerType.INSTANCE, properties, ssr);
        generator.registerExportables(metadata.getDatabase());
        assertEquals(1, metadata.getDatabase().getDefaultNamespace().getTables().size());
        final Table table = metadata.getDatabase().getDefaultNamespace().getTables().iterator().next();
        final String[] createCommands = new DB2Dialect().getTableExporter().getSqlCreateStrings(table, metadata);
        assertContains("sequence_name varchar(255) not null", createCommands[0]);
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : Table(org.hibernate.mapping.Table) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) TableGenerator(org.hibernate.id.enhanced.TableGenerator) MultipleHiLoPerTableGenerator(org.hibernate.id.MultipleHiLoPerTableGenerator) Properties(java.util.Properties) DB2Dialect(org.hibernate.dialect.DB2Dialect) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with TableGenerator

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

the class HiLoTableTest method testNormalBoundary.

@Test
public void testNormalBoundary() {
    EntityPersister persister = sessionFactory().getEntityPersister(Entity.class.getName());
    assertClassAssignability(TableGenerator.class, persister.getIdentifierGenerator().getClass());
    TableGenerator generator = (TableGenerator) persister.getIdentifierGenerator();
    assertClassAssignability(HiLoOptimizer.class, generator.getOptimizer().getClass());
    HiLoOptimizer optimizer = (HiLoOptimizer) generator.getOptimizer();
    int increment = optimizer.getIncrementSize();
    Entity[] entities = new Entity[increment + 1];
    Session s = openSession();
    s.beginTransaction();
    for (int i = 0; i < increment; i++) {
        entities[i] = new Entity("" + (i + 1));
        s.save(entities[i]);
        // initialization
        assertEquals(1, generator.getTableAccessCount());
        // initialization
        assertEquals(1, ((BasicHolder) optimizer.getLastSourceValue()).getActualLongValue());
        assertEquals(i + 1, ((BasicHolder) optimizer.getLastValue()).getActualLongValue());
        assertEquals(increment + 1, ((BasicHolder) optimizer.getHiValue()).getActualLongValue());
    }
    // now force a "clock over"
    entities[increment] = new Entity("" + increment);
    s.save(entities[increment]);
    // initialization
    assertEquals(2, generator.getTableAccessCount());
    // initialization
    assertEquals(2, ((BasicHolder) optimizer.getLastSourceValue()).getActualLongValue());
    assertEquals(increment + 1, ((BasicHolder) optimizer.getLastValue()).getActualLongValue());
    assertEquals((increment * 2) + 1, ((BasicHolder) optimizer.getHiValue()).getActualLongValue());
    s.getTransaction().commit();
    s.beginTransaction();
    for (int i = 0; i < entities.length; 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) HiLoOptimizer(org.hibernate.id.enhanced.HiLoOptimizer) TableGenerator(org.hibernate.id.enhanced.TableGenerator) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with TableGenerator

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

the class BasicTableTest method testNormalBoundary.

@Test
public void testNormalBoundary() {
    EntityPersister persister = sessionFactory().getEntityPersister(Entity.class.getName());
    assertClassAssignability(TableGenerator.class, persister.getIdentifierGenerator().getClass());
    TableGenerator generator = (TableGenerator) 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.getTableAccessCount());
        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) TableGenerator(org.hibernate.id.enhanced.TableGenerator) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with TableGenerator

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

the class PooledTableTest method testNormalBoundary.

@Test
public void testNormalBoundary() {
    EntityPersister persister = sessionFactory().getEntityPersister(Entity.class.getName());
    assertClassAssignability(TableGenerator.class, persister.getIdentifierGenerator().getClass());
    TableGenerator generator = (TableGenerator) persister.getIdentifierGenerator();
    assertClassAssignability(PooledOptimizer.class, generator.getOptimizer().getClass());
    PooledOptimizer optimizer = (PooledOptimizer) generator.getOptimizer();
    int increment = optimizer.getIncrementSize();
    Entity[] entities = new Entity[increment + 2];
    Session s = openSession();
    s.beginTransaction();
    for (int i = 0; i <= increment; i++) {
        entities[i] = new Entity("" + (i + 1));
        s.save(entities[i]);
        // initialization calls seq twice
        assertEquals(2, generator.getTableAccessCount());
        // initialization calls seq twice
        assertEquals(increment + 1, ((BasicHolder) optimizer.getLastSourceValue()).getActualLongValue());
        assertEquals(i + 1, ((BasicHolder) optimizer.getLastValue()).getActualLongValue());
        assertEquals(increment + 1, ((BasicHolder) optimizer.getLastSourceValue()).getActualLongValue());
    }
    // now force a "clock over"
    entities[increment + 1] = new Entity("" + increment);
    s.save(entities[increment + 1]);
    // initialization (2) + clock over
    assertEquals(3, generator.getTableAccessCount());
    // initialization (2) + clock over
    assertEquals((increment * 2) + 1, ((BasicHolder) optimizer.getLastSourceValue()).getActualLongValue());
    assertEquals(increment + 2, ((BasicHolder) optimizer.getLastValue()).getActualLongValue());
    s.getTransaction().commit();
    s.beginTransaction();
    for (int i = 0; i < entities.length; 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) PooledOptimizer(org.hibernate.id.enhanced.PooledOptimizer) TableGenerator(org.hibernate.id.enhanced.TableGenerator) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with TableGenerator

use of org.hibernate.id.enhanced.TableGenerator 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)

Aggregations

TableGenerator (org.hibernate.id.enhanced.TableGenerator)5 Test (org.junit.Test)5 EntityPersister (org.hibernate.persister.entity.EntityPersister)4 Session (org.hibernate.Session)3 PooledOptimizer (org.hibernate.id.enhanced.PooledOptimizer)2 Properties (java.util.Properties)1 Metadata (org.hibernate.boot.Metadata)1 MetadataSources (org.hibernate.boot.MetadataSources)1 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 DB2Dialect (org.hibernate.dialect.DB2Dialect)1 IdentifierGenerator (org.hibernate.id.IdentifierGenerator)1 MultipleHiLoPerTableGenerator (org.hibernate.id.MultipleHiLoPerTableGenerator)1 HiLoOptimizer (org.hibernate.id.enhanced.HiLoOptimizer)1 Table (org.hibernate.mapping.Table)1 TestForIssue (org.hibernate.testing.TestForIssue)1