Search in sources :

Example 6 with SequenceStyleGenerator

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

the class PooledForcedTableSequenceTest 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("pooled optimizer was not used", PooledOptimizer.class.isInstance(generator.getOptimizer()));
    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]);
        long expectedId = i + 1;
        assertEquals(expectedId, entities[i].getId().longValue());
        // NOTE : initialization calls table twice
        assertEquals(2, generator.getDatabaseStructure().getTimesAccessed());
        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]);
    long expectedId = optimizer.getIncrementSize() + 2;
    assertEquals(expectedId, entities[increment + 1].getId().longValue());
    // initialization (2) + clock over
    assertEquals(3, generator.getDatabaseStructure().getTimesAccessed());
    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) TableStructure(org.hibernate.id.enhanced.TableStructure) SequenceStyleGenerator(org.hibernate.id.enhanced.SequenceStyleGenerator) Session(org.hibernate.Session) Test(org.junit.Test)

Example 7 with SequenceStyleGenerator

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

the class BasicSequenceTest method testSequencePerEntity.

@Test
@TestForIssue(jiraKey = "HHH-6790")
public void testSequencePerEntity() {
    final String overriddenEntityName = "SpecialEntity";
    EntityPersister persister = sessionFactory().getEntityPersister(overriddenEntityName);
    assertClassAssignability(SequenceStyleGenerator.class, persister.getIdentifierGenerator().getClass());
    SequenceStyleGenerator generator = (SequenceStyleGenerator) persister.getIdentifierGenerator();
    assertEquals(overriddenEntityName + SequenceStyleGenerator.DEF_SEQUENCE_SUFFIX, generator.getDatabaseStructure().getName());
    Session s = openSession();
    s.beginTransaction();
    Entity entity1 = new Entity("1");
    s.save(overriddenEntityName, entity1);
    Entity entity2 = new Entity("2");
    s.save(overriddenEntityName, entity2);
    s.getTransaction().commit();
    assertEquals(1, entity1.getId().intValue());
    assertEquals(2, entity2.getId().intValue());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SequenceStyleGenerator(org.hibernate.id.enhanced.SequenceStyleGenerator) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 8 with SequenceStyleGenerator

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

the class HiLoSequenceTest method testNormalBoundary.

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

Example 9 with SequenceStyleGenerator

use of org.hibernate.id.enhanced.SequenceStyleGenerator in project invesdwin-context-persistence by subes.

the class OneSequencePerEntityAspectTest method testAspectWorking.

@Test
public void testAspectWorking() {
    final Type type = null;
    final Properties params = new Properties();
    final ServiceRegistry serviceRegistry = null;
    try {
        new SequenceStyleGenerator().configure(type, params, serviceRegistry);
        Assertions.fail("exception expected");
    } catch (final NullPointerException e) {
    // SUPPRESS CHECKSTYLE single line
    // ignore
    }
    Assertions.assertThat(params.get(SequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY)).isEqualTo(true);
}
Also used : Type(org.hibernate.type.Type) ServiceRegistry(org.hibernate.service.ServiceRegistry) SequenceStyleGenerator(org.hibernate.id.enhanced.SequenceStyleGenerator) Properties(java.util.Properties) ATest(de.invesdwin.context.test.ATest) Test(org.junit.Test)

Example 10 with SequenceStyleGenerator

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

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