use of org.hibernate.internal.util.collections.JoinedList in project hibernate-orm by hibernate.
the class UnionSubclassEntityPersister method generateSubquery.
protected String generateSubquery(PersistentClass model, Metadata mapping) {
Dialect dialect = getFactory().getJdbcServices().getDialect();
SqlStringGenerationContext sqlStringGenerationContext = getFactory().getSqlStringGenerationContext();
if (!model.hasSubclasses()) {
return model.getTable().getQualifiedName(sqlStringGenerationContext);
}
Set<Column> columns = new LinkedHashSet<>();
for (Table table : model.getSubclassTableClosure()) {
if (!table.isAbstractUnionTable()) {
columns.addAll(table.getColumns());
}
}
StringBuilder buf = new StringBuilder().append("( ");
List<PersistentClass> classes = new JoinedList<>(List.of(model), Collections.unmodifiableList(model.getSubclasses()));
for (PersistentClass clazz : classes) {
Table table = clazz.getTable();
if (!table.isAbstractUnionTable()) {
// TODO: move to .sql package!!
buf.append("select ");
for (Column col : columns) {
if (!table.containsColumn(col)) {
int sqlType = col.getSqlTypeCode(mapping);
buf.append(dialect.getSelectClauseNullString(sqlType)).append(" as ");
}
buf.append(col.getQuotedName(dialect));
buf.append(", ");
}
buf.append(clazz.getSubclassId()).append(" as clazz_");
buf.append(" from ").append(table.getQualifiedName(sqlStringGenerationContext));
buf.append(" union ");
if (dialect.supportsUnionAll()) {
buf.append("all ");
}
}
}
if (buf.length() > 2) {
// chop the last union (all)
buf.setLength(buf.length() - (dialect.supportsUnionAll() ? 11 : 7));
}
return buf.append(" )").toString();
}
Aggregations