Search in sources :

Example 1 with Index

use of jakarta.persistence.Index in project hibernate-orm by hibernate.

the class JPAXMLOverriddenAnnotationReader method buildIndex.

private static void buildIndex(AnnotationDescriptor annotation, List<JaxbIndex> elements) {
    Index[] indexes = new Index[elements.size()];
    int i = 0;
    for (JaxbIndex element : elements) {
        AnnotationDescriptor indexAnn = new AnnotationDescriptor(Index.class);
        copyAttribute(indexAnn, "name", element.getName(), false);
        copyAttribute(indexAnn, "column-list", element.getColumnList(), true);
        copyAttribute(indexAnn, "unique", element.isUnique(), false);
        indexes[i++] = AnnotationFactory.create(indexAnn);
    }
    annotation.setValue("indexes", indexes);
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) JaxbIndex(org.hibernate.boot.jaxb.mapping.spi.JaxbIndex) JaxbIndex(org.hibernate.boot.jaxb.mapping.spi.JaxbIndex) Index(jakarta.persistence.Index) JaxbUniqueConstraint(org.hibernate.boot.jaxb.mapping.spi.JaxbUniqueConstraint) QueryHint(jakarta.persistence.QueryHint) JaxbQueryHint(org.hibernate.boot.jaxb.mapping.spi.JaxbQueryHint) UniqueConstraint(jakarta.persistence.UniqueConstraint)

Example 2 with Index

use of jakarta.persistence.Index 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));
}
Also used : SequenceGenerator(jakarta.persistence.SequenceGenerator) IdGeneratorStrategyInterpreter(org.hibernate.boot.model.IdGeneratorStrategyInterpreter) UniqueConstraint(jakarta.persistence.UniqueConstraint) Index(jakarta.persistence.Index) TableGenerator(jakarta.persistence.TableGenerator) AnnotatedColumn.buildColumnOrFormulaFromAnnotation(org.hibernate.cfg.AnnotatedColumn.buildColumnOrFormulaFromAnnotation) Annotation(java.lang.annotation.Annotation) GenerationType(jakarta.persistence.GenerationType) GeneratedValue(jakarta.persistence.GeneratedValue) GeneratorNameDeterminationContext(org.hibernate.boot.model.IdGeneratorStrategyInterpreter.GeneratorNameDeterminationContext) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) PersistentClass(org.hibernate.mapping.PersistentClass) XClass(org.hibernate.annotations.common.reflection.XClass)

Aggregations

Index (jakarta.persistence.Index)2 UniqueConstraint (jakarta.persistence.UniqueConstraint)2 GeneratedValue (jakarta.persistence.GeneratedValue)1 GenerationType (jakarta.persistence.GenerationType)1 QueryHint (jakarta.persistence.QueryHint)1 SequenceGenerator (jakarta.persistence.SequenceGenerator)1 TableGenerator (jakarta.persistence.TableGenerator)1 Annotation (java.lang.annotation.Annotation)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 JaxbIndex (org.hibernate.boot.jaxb.mapping.spi.JaxbIndex)1 JaxbQueryHint (org.hibernate.boot.jaxb.mapping.spi.JaxbQueryHint)1 JaxbUniqueConstraint (org.hibernate.boot.jaxb.mapping.spi.JaxbUniqueConstraint)1 IdGeneratorStrategyInterpreter (org.hibernate.boot.model.IdGeneratorStrategyInterpreter)1 GeneratorNameDeterminationContext (org.hibernate.boot.model.IdGeneratorStrategyInterpreter.GeneratorNameDeterminationContext)1 IdentifierGeneratorDefinition (org.hibernate.boot.model.IdentifierGeneratorDefinition)1 AnnotatedColumn.buildColumnOrFormulaFromAnnotation (org.hibernate.cfg.AnnotatedColumn.buildColumnOrFormulaFromAnnotation)1 PersistentClass (org.hibernate.mapping.PersistentClass)1