Search in sources :

Example 1 with Index

use of javax.persistence.Index in project cassandra-driver-mapping by valchkou.

the class EntityTypeParser method parseEntityLevelMetadata.

/**
 * Parses class level annotations and initializes EntityMetadata object for
 * given entity class
 *
 * @param clazz
 * @return EntityMetadata
 */
private static <T> EntityTypeMetadata parseEntityLevelMetadata(Class<T> clazz) {
    EntityTypeMetadata result = null;
    Annotation annotation = clazz.getAnnotation(Table.class);
    if (annotation instanceof Table) {
        Table tableAntn = (Table) annotation;
        String tableName = tableAntn.name();
        if (tableName != null && tableName.length() > 0) {
            result = new EntityTypeMetadata(clazz, tableName);
        } else {
            result = new EntityTypeMetadata(clazz);
        }
        Index[] indexes = tableAntn.indexes();
        if (indexes != null && indexes.length > 0) {
            for (Index index : indexes) {
                result.addindex(index.name(), index.columnList());
            }
        }
    } else {
        result = new EntityTypeMetadata(clazz);
    }
    // parse properties
    annotation = clazz.getAnnotation(TableProperties.class);
    if (annotation instanceof TableProperties) {
        TableProperty[] props = ((TableProperties) annotation).values();
        for (TableProperty prop : props) {
            result.addProperty(prop.value());
        }
    }
    // parse ttl
    annotation = clazz.getAnnotation(Ttl.class);
    if (annotation instanceof Ttl) {
        result.setTtl(((Ttl) annotation).value());
    }
    return result;
}
Also used : EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) Table(javax.persistence.Table) Index(javax.persistence.Index) TableProperties(com.datastax.driver.mapping.annotation.TableProperties) Ttl(com.datastax.driver.mapping.annotation.Ttl) Annotation(java.lang.annotation.Annotation) TableProperty(com.datastax.driver.mapping.annotation.TableProperty)

Example 2 with Index

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

the class JPAOverriddenAnnotationReader method buildIndex.

private static void buildIndex(AnnotationDescriptor annotation, Element element) {
    List indexElementList = element.elements("index");
    Index[] indexes = new Index[indexElementList.size()];
    for (int i = 0; i < indexElementList.size(); i++) {
        Element subelement = (Element) indexElementList.get(i);
        AnnotationDescriptor indexAnn = new AnnotationDescriptor(Index.class);
        copyStringAttribute(indexAnn, subelement, "name", false);
        copyStringAttribute(indexAnn, subelement, "column-list", true);
        copyBooleanAttribute(indexAnn, subelement, "unique");
        indexes[i] = AnnotationFactory.create(indexAnn);
    }
    annotation.setValue("indexes", indexes);
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) List(java.util.List) Index(javax.persistence.Index) QueryHint(javax.persistence.QueryHint) UniqueConstraint(javax.persistence.UniqueConstraint)

Example 3 with Index

use of javax.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 `javax.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 IdGeneratorStrategyInterpreter.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(javax.persistence.SequenceGenerator) IdGeneratorStrategyInterpreter(org.hibernate.boot.model.IdGeneratorStrategyInterpreter) UniqueConstraint(javax.persistence.UniqueConstraint) Index(javax.persistence.Index) TableGenerator(javax.persistence.TableGenerator) MultipleHiLoPerTableGenerator(org.hibernate.id.MultipleHiLoPerTableGenerator) Annotation(java.lang.annotation.Annotation) GenerationType(javax.persistence.GenerationType) GeneratedValue(javax.persistence.GeneratedValue) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) PersistentClass(org.hibernate.mapping.PersistentClass) XClass(org.hibernate.annotations.common.reflection.XClass)

Aggregations

Index (javax.persistence.Index)3 Annotation (java.lang.annotation.Annotation)2 UniqueConstraint (javax.persistence.UniqueConstraint)2 TableProperties (com.datastax.driver.mapping.annotation.TableProperties)1 TableProperty (com.datastax.driver.mapping.annotation.TableProperty)1 Ttl (com.datastax.driver.mapping.annotation.Ttl)1 EntityTypeMetadata (com.datastax.driver.mapping.meta.EntityTypeMetadata)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 GeneratedValue (javax.persistence.GeneratedValue)1 GenerationType (javax.persistence.GenerationType)1 QueryHint (javax.persistence.QueryHint)1 SequenceGenerator (javax.persistence.SequenceGenerator)1 Table (javax.persistence.Table)1 TableGenerator (javax.persistence.TableGenerator)1 Element (org.dom4j.Element)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 IdGeneratorStrategyInterpreter (org.hibernate.boot.model.IdGeneratorStrategyInterpreter)1