Search in sources :

Example 16 with IdentifierGenerator

use of org.hibernate.id.IdentifierGenerator in project hibernate-orm by hibernate.

the class GeneratedValueTests method testImplicitTableGenerator.

@Test
public void testImplicitTableGenerator() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    final Metadata bootModel = new MetadataSources(ssr).addAnnotatedClass(ImplicitTableGeneratorEntity.class).buildMetadata();
    final PersistentClass entityMapping = bootModel.getEntityBinding(ImplicitTableGeneratorEntity.class.getName());
    final IdentifierGenerator generator = entityMapping.getIdentifier().createIdentifierGenerator(bootModel.getIdentifierGeneratorFactory(), ssr.getService(JdbcEnvironment.class).getDialect(), null, null, (RootClass) entityMapping);
    final TableGenerator tableGenerator = assertTyping(TableGenerator.class, generator);
    assertThat(tableGenerator.getTableName(), is("my_id_table"));
    // all the JPA defaults since they were not defined
    assertThat(tableGenerator.getInitialValue(), is(1));
    assertThat(tableGenerator.getIncrementSize(), is(50));
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) TableGenerator(org.hibernate.id.enhanced.TableGenerator) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) Test(org.junit.Test)

Example 17 with IdentifierGenerator

use of org.hibernate.id.IdentifierGenerator in project hibernate-orm by hibernate.

the class GeneratedValueTests method testExplicitTableGeneratorImplicitName.

@Test
public void testExplicitTableGeneratorImplicitName() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    final Metadata bootModel = new MetadataSources(ssr).addAnnotatedClass(ExplicitTableGeneratorImplicitNameEntity.class).buildMetadata();
    final PersistentClass entityMapping = bootModel.getEntityBinding(ExplicitTableGeneratorImplicitNameEntity.class.getName());
    final IdentifierGenerator generator = entityMapping.getIdentifier().createIdentifierGenerator(bootModel.getIdentifierGeneratorFactory(), ssr.getService(JdbcEnvironment.class).getDialect(), null, null, (RootClass) entityMapping);
    final TableGenerator tableGenerator = assertTyping(TableGenerator.class, generator);
    assertThat(tableGenerator.getTableName(), is("my_id_table"));
    // - note : currently initialValue=1 in mapping is shows up here as 2
    assertThat(tableGenerator.getInitialValue(), is(1));
    assertThat(tableGenerator.getIncrementSize(), is(25));
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) TableGenerator(org.hibernate.id.enhanced.TableGenerator) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) Test(org.junit.Test)

Example 18 with IdentifierGenerator

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

use of org.hibernate.id.IdentifierGenerator in project hibernate-orm by hibernate.

the class GeneratedValueTests method testExplicitTableGenerator.

@Test
public void testExplicitTableGenerator() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    final Metadata bootModel = new MetadataSources(ssr).addAnnotatedClass(ExplicitTableGeneratorEntity.class).buildMetadata();
    final PersistentClass entityMapping = bootModel.getEntityBinding(ExplicitTableGeneratorEntity.class.getName());
    final IdentifierGenerator generator = entityMapping.getIdentifier().createIdentifierGenerator(bootModel.getIdentifierGeneratorFactory(), ssr.getService(JdbcEnvironment.class).getDialect(), null, null, (RootClass) entityMapping);
    final TableGenerator tableGenerator = assertTyping(TableGenerator.class, generator);
    assertThat(tableGenerator.getTableName(), is("my_real_id_table"));
    // all the JPA defaults since they were not defined
    // - note : currently initialValue=1 in mapping is shows up here
    // as 2
    // assertThat( tableGenerator.getInitialValue(), is( 1 ) );
    assertThat(tableGenerator.getIncrementSize(), is(25));
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) TableGenerator(org.hibernate.id.enhanced.TableGenerator) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) Test(org.junit.Test)

Example 20 with IdentifierGenerator

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

Aggregations

IdentifierGenerator (org.hibernate.id.IdentifierGenerator)22 Test (org.junit.Test)18 Metadata (org.hibernate.boot.Metadata)11 MetadataSources (org.hibernate.boot.MetadataSources)11 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)11 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)11 PersistentClass (org.hibernate.mapping.PersistentClass)11 SequenceStyleGenerator (org.hibernate.id.enhanced.SequenceStyleGenerator)10 EntityPersister (org.hibernate.persister.entity.EntityPersister)8 TableGenerator (org.hibernate.id.enhanced.TableGenerator)4 BulkInsertionCapableIdentifierGenerator (org.hibernate.id.BulkInsertionCapableIdentifierGenerator)2 NoopOptimizer (org.hibernate.id.enhanced.NoopOptimizer)2 AST (antlr.collections.AST)1 Iterator (java.util.Iterator)1 MappingException (org.hibernate.MappingException)1 QueryException (org.hibernate.QueryException)1 Session (org.hibernate.Session)1 SessionFactory (org.hibernate.SessionFactory)1 MetadataImpl (org.hibernate.boot.internal.MetadataImpl)1 Sequence (org.hibernate.boot.model.relational.Sequence)1