Search in sources :

Example 1 with DiscriminatorColumn

use of javax.persistence.DiscriminatorColumn 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 2 with DiscriminatorColumn

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

the class SqlGenerator method handleDaoAttributes.

protected void handleDaoAttributes(Class<?> clazz) {
    Attribute attr;
    Class<?> current = clazz;
    while (current != null && current.getAnnotation(Entity.class) != null) {
        DiscriminatorColumn column = current.getAnnotation(DiscriminatorColumn.class);
        if (column != null) {
            String columnName = column.name();
            attr = findAttribute(columnName);
            if (attr != null) {
                attr.setTrue(Attribute.Flag.DaoGenerated);
                attr.setTrue(Attribute.Flag.Insertable);
                attr.setTrue(Attribute.Flag.Updatable);
                attr.setFalse(Attribute.Flag.Nullable);
                attr.setTrue(Attribute.Flag.DC);
            } else {
                attr = new Attribute(DbUtil.getTableName(current), column.name());
                attr.setFalse(Flag.Selectable);
                attr.setTrue(Flag.Insertable);
                attr.setTrue(Flag.DaoGenerated);
                attr.setTrue(Flag.DC);
                _attributes.add(attr);
            }
            if (column.discriminatorType() == DiscriminatorType.CHAR) {
                attr.setTrue(Attribute.Flag.CharDT);
            } else if (column.discriminatorType() == DiscriminatorType.STRING) {
                attr.setTrue(Attribute.Flag.StringDT);
            } else if (column.discriminatorType() == DiscriminatorType.INTEGER) {
                attr.setTrue(Attribute.Flag.IntegerDT);
            }
        }
        PrimaryKeyJoinColumn[] pkjcs = DbUtil.getPrimaryKeyJoinColumns(current);
        if (pkjcs != null) {
            for (PrimaryKeyJoinColumn pkjc : pkjcs) {
                String tableName = DbUtil.getTableName(current);
                attr = findAttribute(pkjc.name());
                if (attr == null || !tableName.equals(attr.table)) {
                    Attribute id = new Attribute(DbUtil.getTableName(current), pkjc.name());
                    if (pkjc.referencedColumnName().length() > 0) {
                        attr = findAttribute(pkjc.referencedColumnName());
                        assert (attr != null) : "Couldn't find referenced column name " + pkjc.referencedColumnName();
                    }
                    id.field = attr.field;
                    id.setTrue(Flag.Id);
                    id.setTrue(Flag.Insertable);
                    id.setFalse(Flag.Updatable);
                    id.setFalse(Flag.Nullable);
                    id.setFalse(Flag.Selectable);
                    _attributes.add(id);
                    List<Attribute> attrs = _ids.get(id.table);
                    attrs.add(id);
                }
            }
        }
        current = current.getSuperclass();
    }
    attr = findAttribute(GenericDao.CREATED_COLUMN);
    if (attr != null && attr.field.getType() == Date.class) {
        attr.setTrue(Attribute.Flag.DaoGenerated);
        attr.setTrue(Attribute.Flag.Insertable);
        attr.setFalse(Attribute.Flag.Updatable);
        attr.setFalse(Attribute.Flag.Date);
        attr.setFalse(Attribute.Flag.Time);
        attr.setTrue(Attribute.Flag.TimeStamp);
        attr.setFalse(Attribute.Flag.Nullable);
        attr.setTrue(Attribute.Flag.Created);
    }
    attr = findAttribute(GenericDao.XID_COLUMN);
    if (attr != null && attr.field.getType() == String.class) {
        attr.setTrue(Attribute.Flag.DaoGenerated);
        attr.setTrue(Attribute.Flag.Insertable);
        attr.setFalse(Attribute.Flag.Updatable);
        attr.setFalse(Attribute.Flag.TimeStamp);
        attr.setFalse(Attribute.Flag.Time);
        attr.setFalse(Attribute.Flag.Date);
        attr.setFalse(Attribute.Flag.Nullable);
        attr.setFalse(Attribute.Flag.Removed);
    }
}
Also used : PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) Date(java.util.Date)

Example 3 with DiscriminatorColumn

use of javax.persistence.DiscriminatorColumn 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 DiscriminatorColumn

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

the class JPAOverriddenAnnotationReader method getDiscriminatorColumn.

private DiscriminatorColumn getDiscriminatorColumn(Element tree, XMLContext.Default defaults) {
    Element element = tree != null ? tree.element("discriminator-column") : null;
    if (element != null) {
        AnnotationDescriptor ad = new AnnotationDescriptor(DiscriminatorColumn.class);
        copyStringAttribute(ad, element, "name", false);
        copyStringAttribute(ad, element, "column-definition", false);
        String value = element.attributeValue("discriminator-type");
        DiscriminatorType type = DiscriminatorType.STRING;
        if (value != null) {
            if ("STRING".equals(value)) {
                type = DiscriminatorType.STRING;
            } else if ("CHAR".equals(value)) {
                type = DiscriminatorType.CHAR;
            } else if ("INTEGER".equals(value)) {
                type = DiscriminatorType.INTEGER;
            } else {
                throw new AnnotationException("Unknown DiscrimiatorType in XML: " + value + " (" + SCHEMA_VALIDATION + ")");
            }
        }
        ad.setValue("discriminatorType", type);
        copyIntegerAttribute(ad, element, "length");
        return AnnotationFactory.create(ad);
    } else if (defaults.canUseJavaAnnotations()) {
        return getPhysicalAnnotation(DiscriminatorColumn.class);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) DiscriminatorType(javax.persistence.DiscriminatorType) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) AnnotationException(org.hibernate.AnnotationException)

Example 5 with DiscriminatorColumn

use of javax.persistence.DiscriminatorColumn 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

DiscriminatorColumn (javax.persistence.DiscriminatorColumn)7 DiscriminatorValue (javax.persistence.DiscriminatorValue)4 DiscriminatorType (javax.persistence.DiscriminatorType)3 Pair (com.cloud.utils.Pair)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Element (org.dom4j.Element)1 AnnotationException (org.hibernate.AnnotationException)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