Skip to content

Commit 1bf9d1d

Browse files
authored
Merge pull request #3559 from ebean-orm/feature/findCount-noId
FindCount on entity bean without @id + platform wants column alias
2 parents 7f1fcbc + 6c47107 commit 1bf9d1d

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

ebean-core/src/main/java/io/ebeaninternal/server/query/CQueryBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ <T> CQueryRowCount buildRowCountQuery(OrmQueryRequest<T> request) {
242242
boolean withAgg = false;
243243
if (!countDistinct) {
244244
withAgg = includesAggregation(request, query);
245-
if (!withAgg) {
245+
if (!withAgg && request.descriptor().hasId()) {
246246
// minimise select clause for standard count
247247
query.setSelectId();
248248
useColumnAlias = false;

ebean-test/src/test/java/org/tests/query/TestRowCount.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.tests.model.basic.Customer;
99
import org.tests.model.basic.Order;
1010
import org.tests.model.basic.ResetBasicData;
11+
import org.tests.model.noid.NoIdBean;
1112

1213
import java.util.List;
1314

@@ -101,4 +102,27 @@ void find_count_distinct_multiplePropertiesSameColumn() {
101102
assertThat(count).isGreaterThan(0);
102103
}
103104

105+
@Test
106+
void find_count_noIdProperty() {
107+
LoggedSql.start();
108+
109+
DB.find(NoIdBean.class)
110+
.select("name, subject")
111+
.findCount();
112+
113+
DB.find(NoIdBean.class)
114+
.setDistinct(true)
115+
.select("name, subject")
116+
.findCount();
117+
118+
List<String> sql = LoggedSql.stop();
119+
120+
assertThat(sql.get(0)).contains("select count(*) from noidbean t0");
121+
if (isSqlServer() || isH2() || isMariaDB() || isMySql()) {
122+
assertThat(sql.get(1)).contains("select count(*) from ( select distinct t0.name c0, t0.subject c1 from noidbean t0)");
123+
} else {
124+
assertThat(sql.get(1)).contains("select count(*) from ( select distinct t0.name, t0.subject from noidbean t0)");
125+
}
126+
}
127+
104128
}

0 commit comments

Comments
 (0)