use of jakarta.persistence.TableGenerator in project hibernate-orm by hibernate.
the class AnnotationBinder method bindPackage.
public static void bindPackage(ClassLoaderService cls, String packageName, MetadataBuildingContext context) {
final Package packaze = cls.packageForNameOrNull(packageName);
if (packaze == null) {
return;
}
final XPackage pckg = context.getBootstrapContext().getReflectionManager().toXPackage(packaze);
if (pckg.isAnnotationPresent(SequenceGenerator.class)) {
SequenceGenerator ann = pckg.getAnnotation(SequenceGenerator.class);
IdentifierGeneratorDefinition idGen = buildIdGenerator(ann, context);
context.getMetadataCollector().addIdentifierGenerator(idGen);
if (LOG.isTraceEnabled()) {
LOG.tracev("Add sequence generator with name: {0}", idGen.getName());
}
}
if (pckg.isAnnotationPresent(SequenceGenerators.class)) {
SequenceGenerators ann = pckg.getAnnotation(SequenceGenerators.class);
for (SequenceGenerator tableGenerator : ann.value()) {
context.getMetadataCollector().addIdentifierGenerator(buildIdGenerator(tableGenerator, context));
}
}
if (pckg.isAnnotationPresent(TableGenerator.class)) {
TableGenerator ann = pckg.getAnnotation(TableGenerator.class);
IdentifierGeneratorDefinition idGen = buildIdGenerator(ann, context);
context.getMetadataCollector().addIdentifierGenerator(idGen);
}
if (pckg.isAnnotationPresent(TableGenerators.class)) {
TableGenerators ann = pckg.getAnnotation(TableGenerators.class);
for (TableGenerator tableGenerator : ann.value()) {
context.getMetadataCollector().addIdentifierGenerator(buildIdGenerator(tableGenerator, context));
}
}
handleTypeDescriptorRegistrations(pckg, context);
bindEmbeddableInstantiatorRegistrations(pckg, context);
bindCompositeUserTypeRegistrations(pckg, context);
bindGenericGenerators(pckg, context);
bindQueries(pckg, context);
bindFilterDefs(pckg, context);
}
use of jakarta.persistence.TableGenerator in project hibernate-orm by hibernate.
the class AnnotationBinder method buildGenerators.
private static HashMap<String, IdentifierGeneratorDefinition> buildGenerators(XAnnotatedElement annElt, MetadataBuildingContext context) {
InFlightMetadataCollector metadataCollector = context.getMetadataCollector();
HashMap<String, IdentifierGeneratorDefinition> generators = new HashMap<>();
TableGenerators tableGenerators = annElt.getAnnotation(TableGenerators.class);
if (tableGenerators != null) {
for (TableGenerator tableGenerator : tableGenerators.value()) {
IdentifierGeneratorDefinition idGenerator = buildIdGenerator(tableGenerator, context);
generators.put(idGenerator.getName(), idGenerator);
metadataCollector.addIdentifierGenerator(idGenerator);
}
}
SequenceGenerators sequenceGenerators = annElt.getAnnotation(SequenceGenerators.class);
if (sequenceGenerators != null) {
for (SequenceGenerator sequenceGenerator : sequenceGenerators.value()) {
IdentifierGeneratorDefinition idGenerator = buildIdGenerator(sequenceGenerator, context);
generators.put(idGenerator.getName(), idGenerator);
metadataCollector.addIdentifierGenerator(idGenerator);
}
}
TableGenerator tabGen = annElt.getAnnotation(TableGenerator.class);
SequenceGenerator seqGen = annElt.getAnnotation(SequenceGenerator.class);
GenericGenerator genGen = annElt.getAnnotation(GenericGenerator.class);
if (tabGen != null) {
IdentifierGeneratorDefinition idGen = buildIdGenerator(tabGen, context);
generators.put(idGen.getName(), idGen);
metadataCollector.addIdentifierGenerator(idGen);
}
if (seqGen != null) {
IdentifierGeneratorDefinition idGen = buildIdGenerator(seqGen, context);
generators.put(idGen.getName(), idGen);
metadataCollector.addIdentifierGenerator(idGen);
}
if (genGen != null) {
IdentifierGeneratorDefinition idGen = buildIdGenerator(genGen, context);
generators.put(idGen.getName(), idGen);
metadataCollector.addIdentifierGenerator(idGen);
}
return generators;
}
use of jakarta.persistence.TableGenerator in project hibernate-orm by hibernate.
the class AnnotationBinder method buildIdGenerator.
private static IdentifierGeneratorDefinition buildIdGenerator(java.lang.annotation.Annotation generatorAnn, MetadataBuildingContext context) {
if (generatorAnn == null) {
return null;
}
IdentifierGeneratorDefinition.Builder definitionBuilder = new IdentifierGeneratorDefinition.Builder();
if (generatorAnn instanceof TableGenerator) {
context.getBuildingOptions().getIdGenerationTypeInterpreter().interpretTableGenerator((TableGenerator) generatorAnn, definitionBuilder);
if (LOG.isTraceEnabled()) {
LOG.tracev("Add table generator with name: {0}", definitionBuilder.getName());
}
} else if (generatorAnn instanceof SequenceGenerator) {
context.getBuildingOptions().getIdGenerationTypeInterpreter().interpretSequenceGenerator((SequenceGenerator) generatorAnn, definitionBuilder);
if (LOG.isTraceEnabled()) {
LOG.tracev("Add sequence generator with name: {0}", definitionBuilder.getName());
}
} else if (generatorAnn instanceof GenericGenerator) {
GenericGenerator genGen = (GenericGenerator) generatorAnn;
definitionBuilder.setName(genGen.name());
definitionBuilder.setStrategy(genGen.strategy());
Parameter[] params = genGen.parameters();
for (Parameter parameter : params) {
definitionBuilder.addParam(parameter.name(), parameter.value());
}
if (LOG.isTraceEnabled()) {
LOG.tracev("Add generic generator with name: {0}", definitionBuilder.getName());
}
} else {
throw new AssertionFailure("Unknown Generator annotation: " + generatorAnn);
}
return definitionBuilder.build();
}
use of jakarta.persistence.TableGenerator in project hibernate-orm by hibernate.
the class BinderHelper method getIdentifierGenerator.
private static IdentifierGeneratorDefinition getIdentifierGenerator(String name, XProperty idXProperty, Map<String, IdentifierGeneratorDefinition> localGenerators, MetadataBuildingContext buildingContext) {
if (localGenerators != null) {
final IdentifierGeneratorDefinition result = localGenerators.get(name);
if (result != null) {
return result;
}
}
final IdentifierGeneratorDefinition globalDefinition = buildingContext.getMetadataCollector().getIdentifierGenerator(name);
if (globalDefinition != null) {
return globalDefinition;
}
log.debugf("Could not resolve explicit IdentifierGeneratorDefinition - using implicit interpretation (%s)", name);
// If we were unable to locate an actual matching named generator assume a sequence/table of the given name.
// this really needs access to the `jakarta.persistence.GenerationType` to work completely properly
//
// (the crux of HHH-12122)
// temporarily, in lieu of having access to GenerationType, assume the EnhancedSequenceGenerator
// for the purpose of testing the feasibility of the approach
final GeneratedValue generatedValueAnn = idXProperty.getAnnotation(GeneratedValue.class);
if (generatedValueAnn == null) {
// this should really never happen, but its easy to protect against it...
return new IdentifierGeneratorDefinition("assigned", "assigned");
}
final IdGeneratorStrategyInterpreter generationInterpreter = buildingContext.getBuildingOptions().getIdGenerationTypeInterpreter();
final GenerationType generationType = interpretGenerationType(generatedValueAnn);
if (generationType == null || generationType == GenerationType.SEQUENCE) {
// NOTE : `null` will ultimately be interpreted as "hibernate_sequence"
log.debugf("Building implicit sequence-based IdentifierGeneratorDefinition (%s)", name);
final IdentifierGeneratorDefinition.Builder builder = new IdentifierGeneratorDefinition.Builder();
generationInterpreter.interpretSequenceGenerator(new SequenceGenerator() {
@Override
public String name() {
return name;
}
@Override
public String sequenceName() {
return "";
}
@Override
public String catalog() {
return "";
}
@Override
public String schema() {
return "";
}
@Override
public int initialValue() {
return 1;
}
@Override
public int allocationSize() {
return 50;
}
@Override
public Class<? extends Annotation> annotationType() {
return SequenceGenerator.class;
}
}, builder);
return builder.build();
} else if (generationType == GenerationType.TABLE) {
// NOTE : `null` will ultimately be interpreted as "hibernate_sequence"
log.debugf("Building implicit table-based IdentifierGeneratorDefinition (%s)", name);
final IdentifierGeneratorDefinition.Builder builder = new IdentifierGeneratorDefinition.Builder();
generationInterpreter.interpretTableGenerator(new TableGenerator() {
@Override
public String name() {
return name;
}
@Override
public String table() {
return "";
}
@Override
public int initialValue() {
return 0;
}
@Override
public int allocationSize() {
return 50;
}
@Override
public String catalog() {
return "";
}
@Override
public String schema() {
return "";
}
@Override
public String pkColumnName() {
return "";
}
@Override
public String valueColumnName() {
return "";
}
@Override
public String pkColumnValue() {
return "";
}
@Override
public UniqueConstraint[] uniqueConstraints() {
return new UniqueConstraint[0];
}
@Override
public Index[] indexes() {
return new Index[0];
}
@Override
public Class<? extends Annotation> annotationType() {
return TableGenerator.class;
}
}, builder);
return builder.build();
}
// really AUTO and IDENTITY work the same in this respect, aside from the actual strategy name
final String strategyName;
if (generationType == GenerationType.IDENTITY) {
strategyName = "identity";
} else {
strategyName = generationInterpreter.determineGeneratorName(generationType, new GeneratorNameDeterminationContext() {
@Override
public Class<?> getIdType() {
return buildingContext.getBootstrapContext().getReflectionManager().toClass(idXProperty.getType());
}
@Override
public String getGeneratedValueGeneratorName() {
return generatedValueAnn.generator();
}
});
}
log.debugf("Building implicit generic IdentifierGeneratorDefinition (%s) : %s", name, strategyName);
return new IdentifierGeneratorDefinition(name, strategyName, Collections.singletonMap(IdentifierGenerator.GENERATOR_NAME, name));
}
use of jakarta.persistence.TableGenerator in project hibernate-orm by hibernate.
the class JPAXMLOverriddenMetadataProvider method getDefaults.
@Override
public Map<Object, Object> getDefaults() {
if (!xmlMappingEnabled) {
return Collections.emptyMap();
} else {
if (defaults == null) {
defaults = new HashMap<>();
XMLContext.Default xmlDefaults = xmlContext.getDefaultWithGlobalCatalogAndSchema();
defaults.put("schema", xmlDefaults.getSchema());
defaults.put("catalog", xmlDefaults.getCatalog());
defaults.put("delimited-identifier", xmlDefaults.getDelimitedIdentifier());
defaults.put("cascade-persist", xmlDefaults.getCascadePersist());
List<Class> entityListeners = new ArrayList<Class>();
for (String className : xmlContext.getDefaultEntityListeners()) {
try {
entityListeners.add(classLoaderAccess.classForName(className));
} catch (ClassLoadingException e) {
throw new IllegalStateException("Default entity listener class not found: " + className);
}
}
defaults.put(EntityListeners.class, entityListeners);
for (JaxbEntityMappings entityMappings : xmlContext.getAllDocuments()) {
List<JaxbSequenceGenerator> jaxbSequenceGenerators = entityMappings.getSequenceGenerator();
List<SequenceGenerator> sequenceGenerators = (List<SequenceGenerator>) defaults.get(SequenceGenerator.class);
if (sequenceGenerators == null) {
sequenceGenerators = new ArrayList<>();
defaults.put(SequenceGenerator.class, sequenceGenerators);
}
for (JaxbSequenceGenerator element : jaxbSequenceGenerators) {
sequenceGenerators.add(JPAXMLOverriddenAnnotationReader.buildSequenceGeneratorAnnotation(element));
}
List<JaxbTableGenerator> jaxbTableGenerators = entityMappings.getTableGenerator();
List<TableGenerator> tableGenerators = (List<TableGenerator>) defaults.get(TableGenerator.class);
if (tableGenerators == null) {
tableGenerators = new ArrayList<>();
defaults.put(TableGenerator.class, tableGenerators);
}
for (JaxbTableGenerator element : jaxbTableGenerators) {
tableGenerators.add(JPAXMLOverriddenAnnotationReader.buildTableGeneratorAnnotation(element, xmlDefaults));
}
List<NamedQuery> namedQueries = (List<NamedQuery>) defaults.get(NamedQuery.class);
if (namedQueries == null) {
namedQueries = new ArrayList<>();
defaults.put(NamedQuery.class, namedQueries);
}
List<NamedQuery> currentNamedQueries = JPAXMLOverriddenAnnotationReader.buildNamedQueries(entityMappings.getNamedQuery(), xmlDefaults, classLoaderAccess);
namedQueries.addAll(currentNamedQueries);
List<NamedNativeQuery> namedNativeQueries = (List<NamedNativeQuery>) defaults.get(NamedNativeQuery.class);
if (namedNativeQueries == null) {
namedNativeQueries = new ArrayList<>();
defaults.put(NamedNativeQuery.class, namedNativeQueries);
}
List<NamedNativeQuery> currentNamedNativeQueries = JPAXMLOverriddenAnnotationReader.buildNamedNativeQueries(entityMappings.getNamedNativeQuery(), xmlDefaults, classLoaderAccess);
namedNativeQueries.addAll(currentNamedNativeQueries);
List<SqlResultSetMapping> sqlResultSetMappings = (List<SqlResultSetMapping>) defaults.get(SqlResultSetMapping.class);
if (sqlResultSetMappings == null) {
sqlResultSetMappings = new ArrayList<>();
defaults.put(SqlResultSetMapping.class, sqlResultSetMappings);
}
List<SqlResultSetMapping> currentSqlResultSetMappings = JPAXMLOverriddenAnnotationReader.buildSqlResultsetMappings(entityMappings.getSqlResultSetMapping(), xmlDefaults, classLoaderAccess);
sqlResultSetMappings.addAll(currentSqlResultSetMappings);
List<NamedStoredProcedureQuery> namedStoredProcedureQueries = (List<NamedStoredProcedureQuery>) defaults.get(NamedStoredProcedureQuery.class);
if (namedStoredProcedureQueries == null) {
namedStoredProcedureQueries = new ArrayList<>();
defaults.put(NamedStoredProcedureQuery.class, namedStoredProcedureQueries);
}
List<NamedStoredProcedureQuery> currentNamedStoredProcedureQueries = JPAXMLOverriddenAnnotationReader.buildNamedStoreProcedureQueries(entityMappings.getNamedStoredProcedureQuery(), xmlDefaults, classLoaderAccess);
namedStoredProcedureQueries.addAll(currentNamedStoredProcedureQueries);
}
}
return defaults;
}
}
Aggregations