mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1810 - Whan paging over query with @Aggregation ( grouping by) wrong total count is calculated
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
package org.tests.iud;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.DB;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.PagedList;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
import org.tests.model.carwheel.Car;
|
||||
import org.tests.model.carwheel.Tire;
|
||||
import org.tests.model.carwheel.Wheel;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestCarWheelIud extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
@@ -53,4 +59,44 @@ public class TestCarWheelIud extends BaseTestCase {
|
||||
Ebean.delete(car2);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aggregatePaging() {
|
||||
|
||||
DB.find(Car.class).delete();
|
||||
|
||||
final Car car0 = createCar("Ford", 40);
|
||||
final Car car1 = createCar("Ford", 50);
|
||||
final Car car2 = createCar("Mazda", 12);
|
||||
|
||||
DB.saveAll(asList(car0, car1, car2));
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
final PagedList<Car> pagedList = DB.find(Car.class)
|
||||
.select("brand, totalSold")
|
||||
.setMaxRows(10)
|
||||
.findPagedList();
|
||||
|
||||
final List<Car> list = pagedList.getList();
|
||||
final int count = pagedList.getTotalCount();
|
||||
|
||||
assertThat(list).hasSize(2);
|
||||
assertThat(count).isEqualTo(2);
|
||||
|
||||
final List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
if (isH2() || isPostgres()) {
|
||||
assertThat(sql.get(0)).contains("select t0.brand, sum(t0.sold) from sa_car t0 group by t0.brand limit 10");
|
||||
assertThat(sql.get(1)).contains("select count(*) from ( select t0.brand, sum(t0.sold) from sa_car t0 group by t0.brand)");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Car createCar(String brand, int sold) {
|
||||
Car car0 = new Car();
|
||||
car0.setBrand(brand);
|
||||
car0.setSold(sold);
|
||||
return car0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,28 @@ public class TestAggregationTopLevel extends BaseTestCase {
|
||||
assertThat(result).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void query_count() {
|
||||
|
||||
Query<DMachineStatsAgg> query = DB.find(DMachineStatsAgg.class)
|
||||
.select("date, totalKms")
|
||||
.setMaxRows(10)
|
||||
.having().gt("totalKms", 1)
|
||||
.query();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
query.findCount();
|
||||
query.findList();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
|
||||
if (isH2() || isPostgres()) {
|
||||
assertThat(sql.get(0)).contains("select count(*) from ( select t0.date, sum(t0.total_kms) from d_machine_stats t0 group by t0.date having sum(t0.total_kms) > ?)");
|
||||
assertThat(sql.get(1)).contains("select t0.date, sum(t0.total_kms) from d_machine_stats t0 group by t0.date having sum(t0.total_kms) > ? limit 10");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void query_machineTotalKms_withHaving() {
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.tests.model.carwheel;
|
||||
|
||||
import io.ebean.annotation.Aggregation;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
@@ -21,6 +23,13 @@ public class Car {
|
||||
@Version
|
||||
private int version;
|
||||
|
||||
private String brand;
|
||||
|
||||
private int sold;
|
||||
|
||||
@Aggregation("sum(sold)")
|
||||
private int totalSold;
|
||||
|
||||
@OneToMany(mappedBy = "car", cascade = CascadeType.ALL)
|
||||
private List<Wheel> wheels;
|
||||
|
||||
@@ -40,6 +49,30 @@ public class Car {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
public void setBrand(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
public int getSold() {
|
||||
return sold;
|
||||
}
|
||||
|
||||
public void setSold(int sold) {
|
||||
this.sold = sold;
|
||||
}
|
||||
|
||||
public int getTotalSold() {
|
||||
return totalSold;
|
||||
}
|
||||
|
||||
public void setTotalSold(int totalSold) {
|
||||
this.totalSold = totalSold;
|
||||
}
|
||||
|
||||
public List<Wheel> getWheels() {
|
||||
return wheels;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user