Index: org/apache/openjpa/jdbc/sql/SelectImpl.java =================================================================== --- org/apache/openjpa/jdbc/sql/SelectImpl.java (revision 566280) +++ org/apache/openjpa/jdbc/sql/SelectImpl.java (working copy) @@ -124,6 +124,7 @@ // combined list of selected ids and map of each id to its alias protected final Selects _selects = newSelects(); private List _ordered = null; + private List _grouped = null; // flags private int _flags = 0; @@ -1371,11 +1372,16 @@ public void groupBy(SQLBuffer sql, Joins joins) { getJoins(joins, true); - if (_grouping == null) - _grouping = new SQLBuffer(_dict); - else - _grouping.append(", "); - _grouping.append(sql); + if (_grouped == null || !_grouped.contains(sql.getSQL())) { + if (_grouping == null) { + _grouping = new SQLBuffer(_dict); + _grouped = new ArrayList(); + } else + _grouping.append(", "); + + _grouping.append(sql.getSQL()); + _grouped.add(sql.getSQL()); + } } public void groupBy(String sql) { @@ -1384,11 +1390,16 @@ public void groupBy(String sql, Joins joins) { getJoins(joins, true); - if (_grouping == null) - _grouping = new SQLBuffer(_dict); - else - _grouping.append(", "); - _grouping.append(sql); + if (_grouped == null || !_grouped.contains(sql)) { + if (_grouping == null) { + _grouping = new SQLBuffer(_dict); + _grouped = new ArrayList(); + } else + _grouping.append(", "); + + _grouping.append(sql); + _grouped.add(sql); + } } public void groupBy(Column col) { @@ -1396,13 +1407,17 @@ } public void groupBy(Column col, Joins joins) { - if (_grouping == null) - _grouping = new SQLBuffer(_dict); - else - _grouping.append(", "); - PathJoins pj = getJoins(joins, true); - _grouping.append(getColumnAlias(col, pj)); + if (_grouped == null || !_grouped.contains(getColumnAlias(col, pj))) { + if (_grouping == null) { + _grouping = new SQLBuffer(_dict); + _grouped = new ArrayList(); + } else + _grouping.append(", "); + + _grouping.append(getColumnAlias(col, pj)); + _grouped.add(getColumnAlias(col, pj)); + } } public void groupBy(Column[] cols) { @@ -1410,16 +1425,19 @@ } public void groupBy(Column[] cols, Joins joins) { - if (_grouping == null) - _grouping = new SQLBuffer(_dict); - else - _grouping.append(", "); - PathJoins pj = getJoins(joins, true); for (int i = 0; i < cols.length; i++) { - if (i > 0) - _grouping.append(", "); - _grouping.append(getColumnAlias(cols[i], pj)); + if (_grouped == null + || !_grouped.contains(getColumnAlias(cols[i], pj))) { + if (_grouping == null) { + _grouping = new SQLBuffer(_dict); + _grouped = new ArrayList(); + } else + _grouping.append(", "); + + _grouping.append(getColumnAlias(cols[i], pj)); + _grouped.add(getColumnAlias(cols[i], pj)); + } } }