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