mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Merge remote-tracking branch 'ebean/master' into feature/platform-dependent-annotations
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package com.avaje.ebeaninternal.server.deploy;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
public class DetermineAggPathTest {
|
||||
|
||||
@Test
|
||||
public void path() throws Exception {
|
||||
|
||||
assertThat(DetermineAggPath.path("count(details)")).isEqualTo("details");
|
||||
assertThat(DetermineAggPath.path("count(details )")).isEqualTo("details");
|
||||
|
||||
assertThat(DetermineAggPath.path("count(person.contacts)")).isEqualTo("person.contacts");
|
||||
assertThat(DetermineAggPath.path("sum(details.quantity*details.unitPrice)")).isEqualTo("details.quantity");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void paths_simple() throws Exception {
|
||||
|
||||
DetermineAggPath.Path paths = DetermineAggPath.paths("count(details)");
|
||||
assertThat(paths.paths).isEqualTo(new String[]{"details"});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void paths_nested() throws Exception {
|
||||
|
||||
DetermineAggPath.Path paths = DetermineAggPath.paths("count(person.contacts)");
|
||||
assertThat(paths.paths).isEqualTo(new String[]{"person", "contacts"});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,10 +17,12 @@ public class TestOrderTotalAmountFormula extends BaseTestCase {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> l0 = Ebean.find(Customer.class).select("id, name")
|
||||
.fetch("orders", "status, totalAmount").where().eq("orders.details.product.name", "Desk")
|
||||
.like("contacts.firstName", "Ji%")
|
||||
|
||||
List<Customer> l0 = Ebean.find(Customer.class)
|
||||
.select("id, name")
|
||||
.fetch("orders", "status, totalAmount")
|
||||
.where()
|
||||
.eq("orders.details.product.name", "Desk")
|
||||
.like("contacts.firstName", "Ji%")
|
||||
.findList();
|
||||
|
||||
for (Customer c0 : l0) {
|
||||
|
||||
@@ -11,14 +11,24 @@ public class TEventMany {
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
String many;
|
||||
String description;
|
||||
|
||||
@ManyToOne
|
||||
TEventOne one;
|
||||
TEventOne event;
|
||||
|
||||
int units;
|
||||
|
||||
double amount;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
public TEventMany(String description, int units, double amount) {
|
||||
this.description = description;
|
||||
this.units = units;
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
@@ -27,20 +37,36 @@ public class TEventMany {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMany() {
|
||||
return many;
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setMany(String many) {
|
||||
this.many = many;
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public TEventOne getOne() {
|
||||
return one;
|
||||
public TEventOne getEvent() {
|
||||
return event;
|
||||
}
|
||||
|
||||
public void setOne(TEventOne one) {
|
||||
this.one = one;
|
||||
public void setEvent(TEventOne event) {
|
||||
this.event = event;
|
||||
}
|
||||
|
||||
public int getUnits() {
|
||||
return units;
|
||||
}
|
||||
|
||||
public void setUnits(int units) {
|
||||
this.units = units;
|
||||
}
|
||||
|
||||
public double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(double amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.avaje.tests.model.tevent;
|
||||
|
||||
import com.avaje.ebean.annotation.Aggregation;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
@@ -13,7 +16,7 @@ public class TEventOne {
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
String one;
|
||||
String name;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
@@ -21,8 +24,21 @@ public class TEventOne {
|
||||
@OneToOne
|
||||
TEvent event;
|
||||
|
||||
@OneToMany(mappedBy = "one")
|
||||
List<TEventMany> many;
|
||||
@Aggregation("count(logs.*)")
|
||||
Long count;
|
||||
|
||||
@Aggregation("sum(logs.units)")
|
||||
Double totalUnits;
|
||||
|
||||
@Aggregation("sum(logs.units * logs.amount)")
|
||||
Double totalAmount;
|
||||
|
||||
@OneToMany(mappedBy = "event", cascade = CascadeType.ALL)
|
||||
List<TEventMany> logs;
|
||||
|
||||
public TEventOne(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
@@ -32,12 +48,24 @@ public class TEventOne {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getOne() {
|
||||
return one;
|
||||
public Long getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setOne(String one) {
|
||||
this.one = one;
|
||||
public Double getTotalUnits() {
|
||||
return totalUnits;
|
||||
}
|
||||
|
||||
public Double getTotalAmount() {
|
||||
return totalAmount;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
@@ -56,11 +84,11 @@ public class TEventOne {
|
||||
this.event = event;
|
||||
}
|
||||
|
||||
public List<TEventMany> getMany() {
|
||||
return many;
|
||||
public List<TEventMany> getLogs() {
|
||||
return logs;
|
||||
}
|
||||
|
||||
public void setMany(List<TEventMany> many) {
|
||||
this.many = many;
|
||||
public void setLogs(List<TEventMany> logs) {
|
||||
this.logs = logs;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,20 @@ public class TestAssocOneNullTraverse extends BaseTestCase {
|
||||
Ebean.save(event);
|
||||
|
||||
Ebean.find(TEvent.class)
|
||||
.fetch("one.many")
|
||||
.fetch("one.logs")
|
||||
.findList();
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void testSelectAggregation() {
|
||||
//
|
||||
// Query<TEvent> query = Ebean.find(TEvent.class)
|
||||
// .select("id, name")
|
||||
// .fetch("one", "count");
|
||||
//
|
||||
// query.findList();
|
||||
//
|
||||
// String sql = query.getGeneratedSql();
|
||||
// assertThat(sql).contains("asd");
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.avaje.tests.query.aggregation;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.tevent.TEventMany;
|
||||
import com.avaje.tests.model.tevent.TEventOne;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestAggregationCount extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
TEventOne one = new TEventOne("first");
|
||||
one.getLogs().add(new TEventMany("all", 1, 10));
|
||||
one.getLogs().add(new TEventMany("be", 2, 12.2));
|
||||
one.getLogs().add(new TEventMany("add", 3, 13));
|
||||
Ebean.save(one);
|
||||
|
||||
TEventOne two = new TEventOne("second");
|
||||
two.getLogs().add(new TEventMany("at", 10, 10));
|
||||
two.getLogs().add(new TEventMany("add", 30, 13));
|
||||
two.getLogs().add(new TEventMany("alf", 30, 13));
|
||||
Ebean.save(two);
|
||||
|
||||
|
||||
Query<TEventOne> query = Ebean.find(TEventOne.class)
|
||||
.select("name, count, totalUnits, totalAmount")
|
||||
.where()
|
||||
.startsWith("logs.description", "a")
|
||||
.having()
|
||||
.ge("count", 1)
|
||||
.orderBy().asc("name");
|
||||
|
||||
List<TEventOne> list = query.findList();
|
||||
for (TEventOne eventOne : list) {
|
||||
System.out.println(eventOne.getId() + " " + eventOne.getName() + " count:" + eventOne.getCount() + " units:" + eventOne.getTotalUnits() + " amount:" + eventOne.getTotalAmount());
|
||||
}
|
||||
|
||||
assertThat(list).isNotEmpty();
|
||||
|
||||
String sql = query.getGeneratedSql();
|
||||
assertThat(sql).contains("select t0.id c0, t0.name c1, count(u1.*) c2, sum(u1.units) c3, sum(u1.units * u1.amount) c4 from tevent_one t0");
|
||||
assertThat(sql).contains("from tevent_one t0 join tevent_many u1 on u1.event_id = t0.id ");
|
||||
assertThat(sql).contains("where u1.description like ? ");
|
||||
assertThat(sql).contains(" group by t0.id, t0.name having count(u1.*) >= ? order by t0.name");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user