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);
}
Aggregations