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;
}
use of org.hibernate.id.IdentifierGenerator in project hibernate-orm by hibernate.
the class NewGeneratorMappingsTest method testMinimalTableEntity.
@Test
public void testMinimalTableEntity() {
final EntityPersister persister = sessionFactory().getEntityPersister(MinimalTableEntity.class.getName());
IdentifierGenerator generator = persister.getIdentifierGenerator();
assertTrue(TableGenerator.class.isInstance(generator));
TableGenerator tabGenerator = (TableGenerator) generator;
assertEquals(MinimalTableEntity.TBL_NAME, tabGenerator.getTableName());
assertEquals(TableGenerator.DEF_SEGMENT_COLUMN, tabGenerator.getSegmentColumnName());
assertEquals("MINIMAL_TBL", tabGenerator.getSegmentValue());
assertEquals(TableGenerator.DEF_VALUE_COLUMN, tabGenerator.getValueColumnName());
// 0 is the annotation default, but its expected to be treated as 1
assertEquals(1, tabGenerator.getInitialValue());
// 50 is the annotation default
assertEquals(50, tabGenerator.getIncrementSize());
assertTrue(PooledOptimizer.class.isInstance(tabGenerator.getOptimizer()));
}
use of org.hibernate.id.IdentifierGenerator in project hibernate-orm by hibernate.
the class NewGeneratorMappingsTest method testSequencePerEntity.
@Test
@TestForIssue(jiraKey = "HHH-6790")
public void testSequencePerEntity() {
// Checking first entity.
EntityPersister persister = sessionFactory().getEntityPersister(DedicatedSequenceEntity1.class.getName());
IdentifierGenerator generator = persister.getIdentifierGenerator();
assertTrue(SequenceStyleGenerator.class.isInstance(generator));
SequenceStyleGenerator seqGenerator = (SequenceStyleGenerator) generator;
assertEquals(StringHelper.unqualifyEntityName(DedicatedSequenceEntity1.class.getName()) + DedicatedSequenceEntity1.SEQUENCE_SUFFIX, seqGenerator.getDatabaseStructure().getName());
// Checking second entity.
persister = sessionFactory().getEntityPersister(DedicatedSequenceEntity2.class.getName());
generator = persister.getIdentifierGenerator();
assertTrue(SequenceStyleGenerator.class.isInstance(generator));
seqGenerator = (SequenceStyleGenerator) generator;
assertEquals(DedicatedSequenceEntity2.ENTITY_NAME + DedicatedSequenceEntity1.SEQUENCE_SUFFIX, seqGenerator.getDatabaseStructure().getName());
}
use of org.hibernate.id.IdentifierGenerator in project hibernate-orm by hibernate.
the class NewGeneratorMappingsTest method testMinimalSequenceEntity.
@Test
public void testMinimalSequenceEntity() {
final EntityPersister persister = sessionFactory().getEntityPersister(MinimalSequenceEntity.class.getName());
IdentifierGenerator generator = persister.getIdentifierGenerator();
assertTrue(SequenceStyleGenerator.class.isInstance(generator));
SequenceStyleGenerator seqGenerator = (SequenceStyleGenerator) generator;
assertEquals(MinimalSequenceEntity.SEQ_NAME, seqGenerator.getDatabaseStructure().getName());
// 1 is the annotation default
assertEquals(1, seqGenerator.getDatabaseStructure().getInitialValue());
// 50 is the annotation default
assertEquals(50, seqGenerator.getDatabaseStructure().getIncrementSize());
assertFalse(NoopOptimizer.class.isInstance(seqGenerator.getOptimizer()));
}
use of org.hibernate.id.IdentifierGenerator in project hibernate-orm by hibernate.
the class NewGeneratorMappingsTest method testAutoEntity.
@Test
public void testAutoEntity() {
final EntityPersister persister = sessionFactory().getEntityPersister(AutoEntity.class.getName());
IdentifierGenerator generator = persister.getIdentifierGenerator();
assertTrue(SequenceStyleGenerator.class.isInstance(generator));
SequenceStyleGenerator seqGenerator = (SequenceStyleGenerator) generator;
assertEquals(SequenceStyleGenerator.DEF_SEQUENCE_NAME, seqGenerator.getDatabaseStructure().getName());
assertEquals(SequenceStyleGenerator.DEFAULT_INITIAL_VALUE, seqGenerator.getDatabaseStructure().getInitialValue());
assertEquals(SequenceStyleGenerator.DEFAULT_INCREMENT_SIZE, seqGenerator.getDatabaseStructure().getIncrementSize());
}
Aggregations