Search in sources :

Example 1 with DiscriminatorValue

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

the class AnnotationBinder method processSingleTableDiscriminatorProperties.

/**
 * Process all discriminator-related metadata per rules for "single table" inheritance
 */
private static AnnotatedDiscriminatorColumn processSingleTableDiscriminatorProperties(XClass clazzToProcess, MetadataBuildingContext context, InheritanceState inheritanceState, EntityBinder entityBinder) {
    final boolean isRoot = !inheritanceState.hasParents();
    AnnotatedDiscriminatorColumn discriminatorColumn = null;
    jakarta.persistence.DiscriminatorColumn discAnn = clazzToProcess.getAnnotation(jakarta.persistence.DiscriminatorColumn.class);
    DiscriminatorType discriminatorType = discAnn != null ? discAnn.discriminatorType() : DiscriminatorType.STRING;
    DiscriminatorFormula discFormulaAnn = getOverridableAnnotation(clazzToProcess, DiscriminatorFormula.class, context);
    if (isRoot) {
        discriminatorColumn = buildDiscriminatorColumn(discriminatorType, discAnn, discFormulaAnn, context);
    }
    if (discAnn != null && !isRoot) {
        LOG.invalidDiscriminatorAnnotation(clazzToProcess.getName());
    }
    final String discriminatorValue = clazzToProcess.isAnnotationPresent(DiscriminatorValue.class) ? clazzToProcess.getAnnotation(DiscriminatorValue.class).value() : null;
    entityBinder.setDiscriminatorValue(discriminatorValue);
    DiscriminatorOptions discriminatorOptions = clazzToProcess.getAnnotation(DiscriminatorOptions.class);
    if (discriminatorOptions != null) {
        entityBinder.setForceDiscriminator(discriminatorOptions.force());
        entityBinder.setInsertableDiscriminator(discriminatorOptions.insert());
    }
    return discriminatorColumn;
}
Also used : DiscriminatorType(jakarta.persistence.DiscriminatorType) DiscriminatorColumn(jakarta.persistence.DiscriminatorColumn) DiscriminatorFormula(org.hibernate.annotations.DiscriminatorFormula) DiscriminatorValue(jakarta.persistence.DiscriminatorValue) DiscriminatorOptions(org.hibernate.annotations.DiscriminatorOptions)

Example 2 with DiscriminatorValue

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

the class JPAXMLOverriddenAnnotationReader method getDiscriminatorValue.

private DiscriminatorValue getDiscriminatorValue(ManagedType root, XMLContext.Default defaults) {
    String element = root instanceof JaxbEntity ? ((JaxbEntity) root).getDiscriminatorValue() : null;
    if (element != null) {
        AnnotationDescriptor ad = new AnnotationDescriptor(DiscriminatorValue.class);
        ad.setValue("value", element);
        return AnnotationFactory.create(ad);
    } else if (defaults.canUseJavaAnnotations()) {
        return getPhysicalAnnotation(DiscriminatorValue.class);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) DiscriminatorValue(jakarta.persistence.DiscriminatorValue) JaxbEntity(org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)

Example 3 with DiscriminatorValue

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

the class AnnotationBinder method processJoinedDiscriminatorProperties.

/**
 * Process all discriminator-related metadata per rules for "joined" inheritance
 */
private static AnnotatedDiscriminatorColumn processJoinedDiscriminatorProperties(XClass clazzToProcess, MetadataBuildingContext context, InheritanceState inheritanceState, EntityBinder entityBinder) {
    if (clazzToProcess.isAnnotationPresent(DiscriminatorFormula.class)) {
        throw new MappingException("@DiscriminatorFormula on joined inheritance not supported at this time");
    }
    // DiscriminatorValue handling ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final DiscriminatorValue discriminatorValueAnnotation = clazzToProcess.getAnnotation(DiscriminatorValue.class);
    final String discriminatorValue = discriminatorValueAnnotation != null ? clazzToProcess.getAnnotation(DiscriminatorValue.class).value() : null;
    entityBinder.setDiscriminatorValue(discriminatorValue);
    // DiscriminatorColumn handling ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    final DiscriminatorColumn discriminatorColumnAnnotation = clazzToProcess.getAnnotation(DiscriminatorColumn.class);
    if (!inheritanceState.hasParents()) {
        // we want to process the discriminator column if either:
        // 1) There is an explicit DiscriminatorColumn annotation && we are not told to ignore them
        // 2) There is not an explicit DiscriminatorColumn annotation && we are told to create them implicitly
        final boolean generateDiscriminatorColumn;
        if (discriminatorColumnAnnotation != null) {
            generateDiscriminatorColumn = !context.getBuildingOptions().ignoreExplicitDiscriminatorsForJoinedInheritance();
            if (generateDiscriminatorColumn) {
                LOG.applyingExplicitDiscriminatorColumnForJoined(clazzToProcess.getName(), AvailableSettings.IGNORE_EXPLICIT_DISCRIMINATOR_COLUMNS_FOR_JOINED_SUBCLASS);
            } else {
                LOG.debugf("Ignoring explicit DiscriminatorColumn annotation on: %s", clazzToProcess.getName());
            }
        } else {
            generateDiscriminatorColumn = context.getBuildingOptions().createImplicitDiscriminatorsForJoinedInheritance();
            if (generateDiscriminatorColumn) {
                LOG.debug("Applying implicit DiscriminatorColumn using DiscriminatorColumn defaults");
            } else {
                LOG.debug("Ignoring implicit (absent) DiscriminatorColumn");
            }
        }
        if (generateDiscriminatorColumn) {
            return buildDiscriminatorColumn(discriminatorColumnAnnotation != null ? discriminatorColumnAnnotation.discriminatorType() : DiscriminatorType.STRING, discriminatorColumnAnnotation, null, context);
        }
    } else {
        if (discriminatorColumnAnnotation != null) {
            LOG.invalidDiscriminatorAnnotation(clazzToProcess.getName());
        }
    }
    return null;
}
Also used : DiscriminatorColumn(jakarta.persistence.DiscriminatorColumn) AnnotatedDiscriminatorColumn.buildDiscriminatorColumn(org.hibernate.cfg.AnnotatedDiscriminatorColumn.buildDiscriminatorColumn) DiscriminatorValue(jakarta.persistence.DiscriminatorValue) MappingException(org.hibernate.MappingException)

Aggregations

DiscriminatorValue (jakarta.persistence.DiscriminatorValue)3 DiscriminatorColumn (jakarta.persistence.DiscriminatorColumn)2 DiscriminatorType (jakarta.persistence.DiscriminatorType)1 MappingException (org.hibernate.MappingException)1 DiscriminatorFormula (org.hibernate.annotations.DiscriminatorFormula)1 DiscriminatorOptions (org.hibernate.annotations.DiscriminatorOptions)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1 JaxbEntity (org.hibernate.boot.jaxb.mapping.spi.JaxbEntity)1 AnnotatedDiscriminatorColumn.buildDiscriminatorColumn (org.hibernate.cfg.AnnotatedDiscriminatorColumn.buildDiscriminatorColumn)1