Search in sources :

Example 21 with IdentifierGenerator

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

the class GeneratedValueTests method testImplicitIncrementGenerator.

@Test
public void testImplicitIncrementGenerator() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
    final Metadata bootModel = new MetadataSources(ssr).addAnnotatedClass(ImplicitIncrementGeneratorEntity.class).buildMetadata();
    final PersistentClass entityMapping = bootModel.getEntityBinding(ImplicitIncrementGeneratorEntity.class.getName());
    final IdentifierGenerator generator = entityMapping.getIdentifier().createIdentifierGenerator(bootModel.getIdentifierGeneratorFactory(), ssr.getService(JdbcEnvironment.class).getDialect(), null, null, (RootClass) entityMapping);
    assertTyping(IncrementGenerator.class, generator);
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) Test(org.junit.Test)

Example 22 with IdentifierGenerator

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

the class Component method buildIdentifierGenerator.

private IdentifierGenerator buildIdentifierGenerator(IdentifierGeneratorFactory identifierGeneratorFactory, Dialect dialect, String defaultCatalog, String defaultSchema, RootClass rootClass) throws MappingException {
    final boolean hasCustomGenerator = !DEFAULT_ID_GEN_STRATEGY.equals(getIdentifierGeneratorStrategy());
    if (hasCustomGenerator) {
        return super.createIdentifierGenerator(identifierGeneratorFactory, dialect, defaultCatalog, defaultSchema, rootClass);
    }
    final Class entityClass = rootClass.getMappedClass();
    // what class is the declarer of the composite pk attributes
    final Class attributeDeclarer;
    CompositeNestedGeneratedValueGenerator.GenerationContextLocator locator;
    // various scenarios for which we need to account here
    if (rootClass.getIdentifierMapper() != null) {
        // we have the @IdClass / <composite-id mapped="true"/> case
        attributeDeclarer = resolveComponentClass();
    } else if (rootClass.getIdentifierProperty() != null) {
        // we have the "@EmbeddedId" / <composite-id name="idName"/> case
        attributeDeclarer = resolveComponentClass();
    } else {
        // we have the "straight up" embedded (again the hibernate term) component identifier
        attributeDeclarer = entityClass;
    }
    locator = new StandardGenerationContextLocator(rootClass.getEntityName());
    final CompositeNestedGeneratedValueGenerator generator = new CompositeNestedGeneratedValueGenerator(locator);
    Iterator itr = getPropertyIterator();
    while (itr.hasNext()) {
        final Property property = (Property) itr.next();
        if (property.getValue().isSimpleValue()) {
            final SimpleValue value = (SimpleValue) property.getValue();
            if (DEFAULT_ID_GEN_STRATEGY.equals(value.getIdentifierGeneratorStrategy())) {
                // the StandardGenerationContextLocator
                continue;
            }
            final IdentifierGenerator valueGenerator = value.createIdentifierGenerator(identifierGeneratorFactory, dialect, defaultCatalog, defaultSchema, rootClass);
            generator.addGeneratedValuePlan(new ValueGenerationPlan(valueGenerator, injector(property, attributeDeclarer)));
        }
    }
    return generator;
}
Also used : CompositeNestedGeneratedValueGenerator(org.hibernate.id.CompositeNestedGeneratedValueGenerator) JoinedIterator(org.hibernate.internal.util.collections.JoinedIterator) Iterator(java.util.Iterator) IdentifierGenerator(org.hibernate.id.IdentifierGenerator)

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