Search in sources :

Example 6 with DiscriminatorValue

use of javax.persistence.DiscriminatorValue in project CloudStack-archive by CloudStack-extras.

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)

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