Search in sources :

Example 1 with DiscriminatorValue

use of javax.persistence.DiscriminatorValue in project simplejpa by appoxy.

the class AnnotationManager method putAnnotationInfo.

/**
     * Gets all the annotation info for a particular class and puts it in our annotation info cache.
     *
     * @param c
     * @return
     */
public AnnotationInfo putAnnotationInfo(Class c) {
    {
        Entity entity = (Entity) c.getAnnotation(Entity.class);
        if (entity == null) {
            throw new PersistenceException("Class not marked as an @Entity: " + c.getName());
        }
    }
    AnnotationInfo ai = new AnnotationInfo();
    ai.setClassAnnotations(c.getAnnotations());
    ai.setMainClass(c);
    Class superClass = c;
    Class rootClass = null;
    while ((superClass = superClass.getSuperclass()) != null) {
        MappedSuperclass mappedSuperclass = (MappedSuperclass) superClass.getAnnotation(MappedSuperclass.class);
        Entity entity = (Entity) superClass.getAnnotation(Entity.class);
        Inheritance inheritance = (Inheritance) superClass.getAnnotation(Inheritance.class);
        if (mappedSuperclass != null || entity != null) {
            putProperties(ai, superClass);
            putMethods(ai, superClass);
            if (entity != null) {
                rootClass = superClass;
            }
            putEntityListeners(ai, superClass);
        }
    }
    if (rootClass != null) {
        ai.setRootClass(rootClass);
        DiscriminatorValue dv = (DiscriminatorValue) c.getAnnotation(DiscriminatorValue.class);
        String discriminatorValue;
        if (dv != null) {
            discriminatorValue = dv.value();
            if (discriminatorValue == null) {
                throw new PersistenceException("You must specify a value= for @DiscriminatorValue on " + c.getName());
            }
        } else {
            discriminatorValue = c.getSimpleName();
        }
        ai.setDiscriminatorValue(discriminatorValue);
        discriminatorMap.put(discriminatorValue, ai);
    } else {
        ai.setRootClass(c);
    }
    putTableDeclaration(ai, c);
    putProperties(ai, c);
    putMethods(ai, c);
    if (ai.getIdMethod() == null) {
        throw new PersistenceException("No ID method specified for: " + c.getName());
    }
    putEntityListeners(ai, c);
    getAnnotationMap().put(c.getName(), ai);
    return ai;
}
Also used : Entity(javax.persistence.Entity) MappedSuperclass(javax.persistence.MappedSuperclass) Inheritance(javax.persistence.Inheritance) PersistenceException(javax.persistence.PersistenceException) DiscriminatorValue(javax.persistence.DiscriminatorValue)

Example 2 with DiscriminatorValue

use of javax.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 Ejb3DiscriminatorColumn processSingleTableDiscriminatorProperties(XClass clazzToProcess, MetadataBuildingContext context, InheritanceState inheritanceState, EntityBinder entityBinder) {
    final boolean isRoot = !inheritanceState.hasParents();
    Ejb3DiscriminatorColumn discriminatorColumn = null;
    javax.persistence.DiscriminatorColumn discAnn = clazzToProcess.getAnnotation(javax.persistence.DiscriminatorColumn.class);
    DiscriminatorType discriminatorType = discAnn != null ? discAnn.discriminatorType() : DiscriminatorType.STRING;
    org.hibernate.annotations.DiscriminatorFormula discFormulaAnn = clazzToProcess.getAnnotation(org.hibernate.annotations.DiscriminatorFormula.class);
    if (isRoot) {
        discriminatorColumn = Ejb3DiscriminatorColumn.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 : DiscriminatorColumn(javax.persistence.DiscriminatorColumn) DiscriminatorType(javax.persistence.DiscriminatorType) DiscriminatorFormula(org.hibernate.annotations.DiscriminatorFormula) DiscriminatorValue(javax.persistence.DiscriminatorValue) DiscriminatorOptions(org.hibernate.annotations.DiscriminatorOptions)

Example 3 with DiscriminatorValue

use of javax.persistence.DiscriminatorValue in project cloudstack by apache.

the class SqlGenerator method buildDiscriminatorClause.

/**
     * buildDiscriminatorClause builds the join clause when there are multiple tables.
     *
     * @return
     */
public Pair<StringBuilder, Map<String, Object>> buildDiscriminatorClause() {
    StringBuilder sql = new StringBuilder();
    Map<String, Object> values = new HashMap<String, Object>();
    for (Class<?> table : _tables) {
        DiscriminatorValue dv = table.getAnnotation(DiscriminatorValue.class);
        if (dv != null) {
            Class<?> parent = table.getSuperclass();
            String tableName = DbUtil.getTableName(parent);
            DiscriminatorColumn dc = parent.getAnnotation(DiscriminatorColumn.class);
            assert (dc != null) : "Parent does not have discrminator column: " + parent.getName();
            sql.append(tableName);
            sql.append(".");
            sql.append(dc.name()).append("=");
            Object value = null;
            if (dc.discriminatorType() == DiscriminatorType.INTEGER) {
                sql.append(dv.value());
                value = Integer.parseInt(dv.value());
            } else if (dc.discriminatorType() == DiscriminatorType.CHAR) {
                sql.append(dv.value());
                value = dv.value().charAt(0);
            } else if (dc.discriminatorType() == DiscriminatorType.STRING) {
                String v = dv.value();
                v = v.substring(0, v.length() < dc.length() ? v.length() : dc.length());
                sql.append("'").append(v).append("'");
                value = v;
            }
            values.put(dc.name(), value);
            sql.append(" AND ");
        }
    }
    return new Pair<StringBuilder, Map<String, Object>>(sql, values);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) DiscriminatorValue(javax.persistence.DiscriminatorValue) Pair(com.cloud.utils.Pair)

Example 4 with DiscriminatorValue

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

the class JPAOverriddenAnnotationReader method getDiscriminatorValue.

private DiscriminatorValue getDiscriminatorValue(Element tree, XMLContext.Default defaults) {
    Element element = tree != null ? tree.element("discriminator-value") : null;
    if (element != null) {
        AnnotationDescriptor ad = new AnnotationDescriptor(DiscriminatorValue.class);
        copyStringElement(element, ad, "value");
        return AnnotationFactory.create(ad);
    } else if (defaults.canUseJavaAnnotations()) {
        return getPhysicalAnnotation(DiscriminatorValue.class);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) DiscriminatorValue(javax.persistence.DiscriminatorValue)

Example 5 with DiscriminatorValue

use of javax.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 Ejb3DiscriminatorColumn 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) {
            if (context.getBuildingOptions().ignoreExplicitDiscriminatorsForJoinedInheritance()) {
                LOG.debugf("Ignoring explicit DiscriminatorColumn annotation on ", clazzToProcess.getName());
                generateDiscriminatorColumn = false;
            } else {
                LOG.applyingExplicitDiscriminatorColumnForJoined(clazzToProcess.getName(), AvailableSettings.IGNORE_EXPLICIT_DISCRIMINATOR_COLUMNS_FOR_JOINED_SUBCLASS);
                generateDiscriminatorColumn = true;
            }
        } else {
            if (context.getBuildingOptions().createImplicitDiscriminatorsForJoinedInheritance()) {
                LOG.debug("Applying implicit DiscriminatorColumn using DiscriminatorColumn defaults");
                generateDiscriminatorColumn = true;
            } else {
                LOG.debug("Ignoring implicit (absent) DiscriminatorColumn");
                generateDiscriminatorColumn = false;
            }
        }
        if (generateDiscriminatorColumn) {
            final DiscriminatorType discriminatorType = discriminatorColumnAnnotation != null ? discriminatorColumnAnnotation.discriminatorType() : DiscriminatorType.STRING;
            return Ejb3DiscriminatorColumn.buildDiscriminatorColumn(discriminatorType, discriminatorColumnAnnotation, null, context);
        }
    } else {
        if (discriminatorColumnAnnotation != null) {
            LOG.invalidDiscriminatorAnnotation(clazzToProcess.getName());
        }
    }
    return null;
}
Also used : DiscriminatorType(javax.persistence.DiscriminatorType) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) DiscriminatorValue(javax.persistence.DiscriminatorValue) MappingException(org.hibernate.MappingException)

Aggregations

DiscriminatorValue (javax.persistence.DiscriminatorValue)6 DiscriminatorColumn (javax.persistence.DiscriminatorColumn)4 Pair (com.cloud.utils.Pair)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 DiscriminatorType (javax.persistence.DiscriminatorType)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Entity (javax.persistence.Entity)1 Inheritance (javax.persistence.Inheritance)1 MappedSuperclass (javax.persistence.MappedSuperclass)1 PersistenceException (javax.persistence.PersistenceException)1 Element (org.dom4j.Element)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