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();
}
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());
}
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();
}
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);
}
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));
}
Aggregations