Search in sources :

Example 1 with OverridesAnnotation

use of org.hibernate.annotations.DialectOverride.OverridesAnnotation in project hibernate-orm by hibernate.

the class AnnotationBinder method getOverridableAnnotation.

public static <T extends Annotation> T getOverridableAnnotation(XAnnotatedElement element, Class<T> annotationType, MetadataBuildingContext context) {
    Dialect dialect = context.getMetadataCollector().getDatabase().getDialect();
    Iterator<Annotation> annotations = Arrays.stream(element.getAnnotations()).flatMap(annotation -> {
        try {
            Method value = annotation.annotationType().getDeclaredMethod("value");
            Class<?> returnType = value.getReturnType();
            if (returnType.isArray() && returnType.getComponentType().isAnnotationPresent(Repeatable.class) && returnType.getComponentType().isAnnotationPresent(OverridesAnnotation.class)) {
                return Stream.of((Annotation[]) value.invoke(annotation));
            }
        } catch (NoSuchMethodException ignored) {
        } catch (Exception e) {
            throw new AssertionFailure("could not read @DialectOverride annotation", e);
        }
        return Stream.of(annotation);
    }).iterator();
    while (annotations.hasNext()) {
        Annotation annotation = annotations.next();
        Class<? extends Annotation> type = annotation.annotationType();
        OverridesAnnotation overridesAnnotation = type.getAnnotation(OverridesAnnotation.class);
        if (overridesAnnotation != null && overridesAnnotation.value().equals(annotationType)) {
            try {
                // noinspection unchecked
                Class<? extends Dialect> overrideDialect = (Class<? extends Dialect>) type.getDeclaredMethod("dialect").invoke(annotation);
                if (overrideDialect.isAssignableFrom(dialect.getClass())) {
                    DialectOverride.Version before = (DialectOverride.Version) type.getDeclaredMethod("before").invoke(annotation);
                    DialectOverride.Version sameOrAfter = (DialectOverride.Version) type.getDeclaredMethod("sameOrAfter").invoke(annotation);
                    if (dialect.getVersion().isBefore(before.major(), before.minor()) && dialect.getVersion().isSameOrAfter(sameOrAfter.major(), sameOrAfter.minor())) {
                        // noinspection unchecked
                        return (T) type.getDeclaredMethod("override").invoke(annotation);
                    }
                }
            } catch (Exception e) {
                throw new AssertionFailure("could not read @DialectOverride annotation", e);
            }
        }
    }
    return element.getAnnotation(annotationType);
}
Also used : Property(org.hibernate.mapping.Property) JavaTypeRegistrations(org.hibernate.annotations.JavaTypeRegistrations) DiscriminatorType(jakarta.persistence.DiscriminatorType) JdbcTypeRegistration(org.hibernate.annotations.JdbcTypeRegistration) Table(jakarta.persistence.Table) ReflectionManager(org.hibernate.annotations.common.reflection.ReflectionManager) BinderHelper.getMappedSuperclassOrNull(org.hibernate.cfg.BinderHelper.getMappedSuperclassOrNull) Map(java.util.Map) QueryBinder(org.hibernate.cfg.annotations.QueryBinder) PropertyBinder(org.hibernate.cfg.annotations.PropertyBinder) EnumSet(java.util.EnumSet) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) FetchType(jakarta.persistence.FetchType) IdentifierGenerator(org.hibernate.id.IdentifierGenerator) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) AttributeOverrides(jakarta.persistence.AttributeOverrides) CollectionBinder(org.hibernate.cfg.annotations.CollectionBinder) JoinedSubclass(org.hibernate.mapping.JoinedSubclass) JavaTypeRegistration(org.hibernate.annotations.JavaTypeRegistration) EntityBinder(org.hibernate.cfg.annotations.EntityBinder) Member(java.lang.reflect.Member) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) Subclass(org.hibernate.mapping.Subclass) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) ManagedBean(org.hibernate.resource.beans.spi.ManagedBean) MapKey(jakarta.persistence.MapKey) Stream(java.util.stream.Stream) GenericGenerators(org.hibernate.annotations.GenericGenerators) Dialect(org.hibernate.dialect.Dialect) ConstraintMode(jakarta.persistence.ConstraintMode) HCANNHelper(org.hibernate.cfg.annotations.HCANNHelper) FetchMode(org.hibernate.FetchMode) JoinTable(jakarta.persistence.JoinTable) NullableDiscriminatorColumnSecondPass(org.hibernate.cfg.internal.NullableDiscriminatorColumnSecondPass) OrderBy(org.hibernate.annotations.OrderBy) Inheritance(jakarta.persistence.Inheritance) AssertionFailure(org.hibernate.AssertionFailure) SortNatural(org.hibernate.annotations.SortNatural) CustomType(org.hibernate.type.CustomType) CompositeType(org.hibernate.annotations.CompositeType) PropertyAccessStrategy(org.hibernate.property.access.spi.PropertyAccessStrategy) CompositeTypeRegistrations(org.hibernate.annotations.CompositeTypeRegistrations) NotFound(org.hibernate.annotations.NotFound) Embedded(jakarta.persistence.Embedded) SequenceGenerator(jakarta.persistence.SequenceGenerator) PropertyHolderBuilder.buildPropertyHolder(org.hibernate.cfg.PropertyHolderBuilder.buildPropertyHolder) AttributeConverter(jakarta.persistence.AttributeConverter) BasicJavaType(org.hibernate.type.descriptor.java.BasicJavaType) MetadataBuildingContext(org.hibernate.boot.spi.MetadataBuildingContext) SqlResultSetMappings(jakarta.persistence.SqlResultSetMappings) AnnotatedColumn.buildColumnFromAnnotation(org.hibernate.cfg.AnnotatedColumn.buildColumnFromAnnotation) StringHelper.nullIfEmpty(org.hibernate.internal.util.StringHelper.nullIfEmpty) CompositeUserType(org.hibernate.usertype.CompositeUserType) ElementCollection(jakarta.persistence.ElementCollection) XClass(org.hibernate.annotations.common.reflection.XClass) IdGeneratorType(org.hibernate.annotations.IdGeneratorType) FilterDefs(org.hibernate.annotations.FilterDefs) OnDelete(org.hibernate.annotations.OnDelete) Entity(jakarta.persistence.Entity) Proxy(org.hibernate.annotations.Proxy) ForeignKey(org.hibernate.annotations.ForeignKey) Filter(org.hibernate.annotations.Filter) CollectionTypeRegistrations(org.hibernate.annotations.CollectionTypeRegistrations) MapKeyColumn(jakarta.persistence.MapKeyColumn) FetchProfile(org.hibernate.annotations.FetchProfile) OneToOne(jakarta.persistence.OneToOne) Locale(java.util.Locale) OverridesAnnotation(org.hibernate.annotations.DialectOverride.OverridesAnnotation) NamedStoredProcedureQueries(jakarta.persistence.NamedStoredProcedureQueries) InFlightMetadataCollector(org.hibernate.boot.spi.InFlightMetadataCollector) AnnotationException(org.hibernate.AnnotationException) Method(java.lang.reflect.Method) BinderHelper.makeIdGenerator(org.hibernate.cfg.BinderHelper.makeIdGenerator) AnnotatedJoinColumn.buildJoinTableJoinColumns(org.hibernate.cfg.AnnotatedJoinColumn.buildJoinTableJoinColumns) MapKeyJoinColumn(jakarta.persistence.MapKeyJoinColumn) MapKeyJavaType(org.hibernate.annotations.MapKeyJavaType) CollectionBinder.getCollectionBinder(org.hibernate.cfg.annotations.CollectionBinder.getCollectionBinder) JoinColumn(jakarta.persistence.JoinColumn) EmbeddableInstantiatorRegistration(org.hibernate.annotations.EmbeddableInstantiatorRegistration) CallbackType(org.hibernate.jpa.event.spi.CallbackType) Index(org.hibernate.annotations.Index) LazyToOne(org.hibernate.annotations.LazyToOne) FilterDefinition(org.hibernate.engine.spi.FilterDefinition) Annotation(java.lang.annotation.Annotation) PropertyAccessStrategyCompositeUserTypeImpl(org.hibernate.property.access.internal.PropertyAccessStrategyCompositeUserTypeImpl) XMethod(org.hibernate.annotations.common.reflection.XMethod) NamedNativeQueries(jakarta.persistence.NamedNativeQueries) BinderHelper.getPropertyOverriddenByMapperOrMapsId(org.hibernate.cfg.BinderHelper.getPropertyOverriddenByMapperOrMapsId) TableBinder(org.hibernate.cfg.annotations.TableBinder) OptimisticLockStyle(org.hibernate.engine.OptimisticLockStyle) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) PropertyPath(org.hibernate.loader.PropertyPath) OneToMany(jakarta.persistence.OneToMany) RootClass(org.hibernate.mapping.RootClass) ManyToOne(jakarta.persistence.ManyToOne) Formula(org.hibernate.annotations.Formula) CallbackDefinitionResolverLegacyImpl(org.hibernate.jpa.event.internal.CallbackDefinitionResolverLegacyImpl) HashSet(java.util.HashSet) TableGenerator(jakarta.persistence.TableGenerator) InheritanceState.getInheritanceStateOfSuperEntity(org.hibernate.cfg.InheritanceState.getInheritanceStateOfSuperEntity) Repeatable(java.lang.annotation.Repeatable) UniqueConstraint(jakarta.persistence.UniqueConstraint) JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) Fetch(org.hibernate.annotations.Fetch) JoinColumns(jakarta.persistence.JoinColumns) IdGeneratorStrategyInterpreter(org.hibernate.boot.model.IdGeneratorStrategyInterpreter) LazyGroup(org.hibernate.annotations.LazyGroup) Filters(org.hibernate.annotations.Filters) InheritanceType(jakarta.persistence.InheritanceType) Check(org.hibernate.annotations.Check) FilterDef(org.hibernate.annotations.FilterDef) Embeddable(jakarta.persistence.Embeddable) GeneratedValue(jakarta.persistence.GeneratedValue) Comment(org.hibernate.annotations.Comment) MapKeyJoinColumnDelegator(org.hibernate.cfg.annotations.MapKeyJoinColumnDelegator) MapKeyJdbcTypeCode(org.hibernate.annotations.MapKeyJdbcTypeCode) EntityTableXref(org.hibernate.boot.spi.InFlightMetadataCollector.EntityTableXref) DEFAULT_ID_GEN_STRATEGY(org.hibernate.mapping.SimpleValue.DEFAULT_ID_GEN_STRATEGY) Column(jakarta.persistence.Column) OnDeleteAction(org.hibernate.annotations.OnDeleteAction) NamedNativeQuery(jakarta.persistence.NamedNativeQuery) NamedStoredProcedureQuery(jakarta.persistence.NamedStoredProcedureQuery) EmbeddableInstantiator(org.hibernate.metamodel.spi.EmbeddableInstantiator) Version(jakarta.persistence.Version) Arrays(java.util.Arrays) MapKeyMutability(org.hibernate.annotations.MapKeyMutability) CoreLogging.messageLogger(org.hibernate.internal.CoreLogging.messageLogger) CollectionTable(jakarta.persistence.CollectionTable) PersistentClass(org.hibernate.mapping.PersistentClass) Parent(org.hibernate.annotations.Parent) AnnotatedJoinColumn.buildJoinColumnsWithDefaultColumnSuffix(org.hibernate.cfg.AnnotatedJoinColumn.buildJoinColumnsWithDefaultColumnSuffix) NamedQuery(jakarta.persistence.NamedQuery) AttributeOverride(jakarta.persistence.AttributeOverride) ManyToMany(jakarta.persistence.ManyToMany) PrimaryKeyJoinColumns(jakarta.persistence.PrimaryKeyJoinColumns) CompositeTypeRegistration(org.hibernate.annotations.CompositeTypeRegistration) IdClass(jakarta.persistence.IdClass) Parameter(org.hibernate.annotations.Parameter) SortComparator(org.hibernate.annotations.SortComparator) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) XAnnotatedElement(org.hibernate.annotations.common.reflection.XAnnotatedElement) NamedQueries(jakarta.persistence.NamedQueries) StringHelper(org.hibernate.internal.util.StringHelper) Set(java.util.Set) Id(jakarta.persistence.Id) UnionSubclass(org.hibernate.mapping.UnionSubclass) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParamDef(org.hibernate.annotations.ParamDef) CollectionTypeRegistration(org.hibernate.annotations.CollectionTypeRegistration) DiscriminatorFormula(org.hibernate.annotations.DiscriminatorFormula) MappedSuperclass(jakarta.persistence.MappedSuperclass) HibernateException(org.hibernate.HibernateException) MapKeyJdbcType(org.hibernate.annotations.MapKeyJdbcType) JavaType(org.hibernate.type.descriptor.java.JavaType) ArrayList(java.util.ArrayList) DialectOverride(org.hibernate.annotations.DialectOverride) Columns(org.hibernate.annotations.Columns) PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) XPackage(org.hibernate.annotations.common.reflection.XPackage) InheritanceState.getSuperclassInheritanceState(org.hibernate.cfg.InheritanceState.getSuperclassInheritanceState) EmbeddableInstantiatorRegistrations(org.hibernate.annotations.EmbeddableInstantiatorRegistrations) XProperty(org.hibernate.annotations.common.reflection.XProperty) AnnotatedColumn.buildFormulaFromAnnotation(org.hibernate.cfg.AnnotatedColumn.buildFormulaFromAnnotation) Cache(org.hibernate.annotations.Cache) NaturalId(org.hibernate.annotations.NaturalId) UserType(org.hibernate.usertype.UserType) ManagedBeanRegistry(org.hibernate.resource.beans.spi.ManagedBeanRegistry) MapKeyCustomType(org.hibernate.annotations.MapKeyCustomType) NotFoundAction(org.hibernate.annotations.NotFoundAction) FetchProfiles(org.hibernate.annotations.FetchProfiles) SingleTableSubclass(org.hibernate.mapping.SingleTableSubclass) Any(org.hibernate.mapping.Any) IdentifierGeneratorCreator(org.hibernate.mapping.IdentifierGeneratorCreator) ToOne(org.hibernate.mapping.ToOne) JdbcTypeRegistrations(org.hibernate.annotations.JdbcTypeRegistrations) DiscriminatorOptions(org.hibernate.annotations.DiscriminatorOptions) Join(org.hibernate.mapping.Join) SqlResultSetMapping(jakarta.persistence.SqlResultSetMapping) MapKeyColumnDelegator(org.hibernate.cfg.annotations.MapKeyColumnDelegator) Source(org.hibernate.annotations.Source) PropertyAccessStrategyMixedImpl(org.hibernate.property.access.internal.PropertyAccessStrategyMixedImpl) DiscriminatorValue(jakarta.persistence.DiscriminatorValue) SimpleValue(org.hibernate.mapping.SimpleValue) DependantValue(org.hibernate.mapping.DependantValue) Cascade(org.hibernate.annotations.Cascade) OrderColumn(jakarta.persistence.OrderColumn) EmbeddedId(jakarta.persistence.EmbeddedId) BatchSize(org.hibernate.annotations.BatchSize) ManyToAny(org.hibernate.annotations.ManyToAny) SequenceGenerators(jakarta.persistence.SequenceGenerators) List(java.util.List) Basic(jakarta.persistence.Basic) Type(java.lang.reflect.Type) MappingException(org.hibernate.MappingException) AnnotatedColumn.buildColumnsFromAnnotations(org.hibernate.cfg.AnnotatedColumn.buildColumnsFromAnnotations) BasicValue(org.hibernate.mapping.BasicValue) TableGenerators(jakarta.persistence.TableGenerators) MapKeyJoinColumns(jakarta.persistence.MapKeyJoinColumns) MapsId(jakarta.persistence.MapsId) CoreMessageLogger(org.hibernate.internal.CoreMessageLogger) HashMap(java.util.HashMap) CascadeType(org.hibernate.annotations.CascadeType) DiscriminatorColumn(jakarta.persistence.DiscriminatorColumn) ListIndexBase(org.hibernate.annotations.ListIndexBase) LazyToOneOption(org.hibernate.annotations.LazyToOneOption) ConvertedJdbcMapping(org.hibernate.cfg.internal.ConvertedJdbcMapping) CustomIdGeneratorCreationContext(org.hibernate.id.factory.spi.CustomIdGeneratorCreationContext) Constraint(org.hibernate.mapping.Constraint) Iterator(java.util.Iterator) GenericGenerator(org.hibernate.annotations.GenericGenerator) Where(org.hibernate.annotations.Where) Component(org.hibernate.mapping.Component) AnnotatedDiscriminatorColumn.buildDiscriminatorColumn(org.hibernate.cfg.AnnotatedDiscriminatorColumn.buildDiscriminatorColumn) Nullability(org.hibernate.cfg.annotations.Nullability) Collections(java.util.Collections) CollectionId(org.hibernate.annotations.CollectionId) AnnotatedColumn.buildColumnFromNoAnnotation(org.hibernate.cfg.AnnotatedColumn.buildColumnFromNoAnnotation) KeyValue(org.hibernate.mapping.KeyValue) Repeatable(java.lang.annotation.Repeatable) AssertionFailure(org.hibernate.AssertionFailure) Method(java.lang.reflect.Method) XMethod(org.hibernate.annotations.common.reflection.XMethod) OverridesAnnotation(org.hibernate.annotations.DialectOverride.OverridesAnnotation) AnnotatedColumn.buildColumnFromAnnotation(org.hibernate.cfg.AnnotatedColumn.buildColumnFromAnnotation) OverridesAnnotation(org.hibernate.annotations.DialectOverride.OverridesAnnotation) Annotation(java.lang.annotation.Annotation) AnnotatedColumn.buildFormulaFromAnnotation(org.hibernate.cfg.AnnotatedColumn.buildFormulaFromAnnotation) AnnotatedColumn.buildColumnFromNoAnnotation(org.hibernate.cfg.AnnotatedColumn.buildColumnFromNoAnnotation) AnnotationException(org.hibernate.AnnotationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) MappingException(org.hibernate.MappingException) Version(jakarta.persistence.Version) Dialect(org.hibernate.dialect.Dialect) XClass(org.hibernate.annotations.common.reflection.XClass) RootClass(org.hibernate.mapping.RootClass) PersistentClass(org.hibernate.mapping.PersistentClass) IdClass(jakarta.persistence.IdClass) DialectOverride(org.hibernate.annotations.DialectOverride)

Aggregations

AttributeConverter (jakarta.persistence.AttributeConverter)1 AttributeOverride (jakarta.persistence.AttributeOverride)1 AttributeOverrides (jakarta.persistence.AttributeOverrides)1 Basic (jakarta.persistence.Basic)1 CollectionTable (jakarta.persistence.CollectionTable)1 Column (jakarta.persistence.Column)1 ConstraintMode (jakarta.persistence.ConstraintMode)1 DiscriminatorColumn (jakarta.persistence.DiscriminatorColumn)1 DiscriminatorType (jakarta.persistence.DiscriminatorType)1 DiscriminatorValue (jakarta.persistence.DiscriminatorValue)1 ElementCollection (jakarta.persistence.ElementCollection)1 Embeddable (jakarta.persistence.Embeddable)1 Embedded (jakarta.persistence.Embedded)1 EmbeddedId (jakarta.persistence.EmbeddedId)1 Entity (jakarta.persistence.Entity)1 FetchType (jakarta.persistence.FetchType)1 GeneratedValue (jakarta.persistence.GeneratedValue)1 Id (jakarta.persistence.Id)1 IdClass (jakarta.persistence.IdClass)1 Inheritance (jakarta.persistence.Inheritance)1