Example usage for javax.persistence DiscriminatorType STRING

List of usage examples for javax.persistence DiscriminatorType STRING

Introduction

In this page you can find the example usage for javax.persistence DiscriminatorType STRING.

Prototype

DiscriminatorType STRING

To view the source code for javax.persistence DiscriminatorType STRING.

Click Source Link

Document

String as the discriminator type.

Usage

From source file:com.cloud.utils.db.SqlGenerator.java

protected void handleDaoAttributes(Class<?> clazz) {
    Attribute attr;//  ww  w .  j  a v  a  2 s  .  co  m
    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);
    }
}

From source file:com.cloud.utils.db.SqlGenerator.java

/**
 * buildDiscriminatorClause builds the join clause when there are multiple tables.
 *
 * @return//from   ww w  .  jav a 2  s . c om
 */
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);
}

From source file:org.apache.openjpa.persistence.jdbc.XMLPersistenceMappingParser.java

private void parseDiscriminatorColumn(Attributes attrs) {
    String val = attrs.getValue("discriminator-type");
    if (val != null) {
        _discType = Enum.valueOf(DiscriminatorType.class, val);
    } else {/*from   w  ww. ja  v  a2  s  . c om*/
        _discType = DiscriminatorType.STRING;
    }

}