mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1737 - PersistenceException while using aggregate query on related entity
This part includes support for joins off an aggregation formula
on a ManyToOne.
e.g. We use min(customer) ... to 'query join' to customer
in the query below.
List<Contact> contacts = Ebean.find(Contact.class)
.select("lastName, min(customer)")
.fetchQuery("customer", "name, status")
.findList();
This commit is contained in:
@@ -29,6 +29,8 @@ public class SqlTreeProperties {
|
||||
|
||||
private boolean allProperties;
|
||||
|
||||
private boolean aggregationManyToOne;
|
||||
|
||||
private boolean aggregation;
|
||||
|
||||
private String aggregationPath;
|
||||
@@ -47,6 +49,15 @@ public class SqlTreeProperties {
|
||||
public void add(STreeProperty prop) {
|
||||
propsList.add(prop);
|
||||
propNames.add(prop.getName());
|
||||
if (prop.isAggregation()) {
|
||||
if (!aggregation) {
|
||||
aggregation = true;
|
||||
aggregationPath = prop.getElPrefix();
|
||||
}
|
||||
if (prop.isAggregationManyToOne()) {
|
||||
aggregationManyToOne = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public STreeProperty[] getProps() {
|
||||
@@ -85,6 +96,13 @@ public class SqlTreeProperties {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this is an aggregation formula on a ManyToOne.
|
||||
*/
|
||||
boolean isAggregationManyToOne() {
|
||||
return aggregationManyToOne;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this contains an aggregation property.
|
||||
*/
|
||||
@@ -92,33 +110,17 @@ public class SqlTreeProperties {
|
||||
return aggregation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for aggregation (need for groug by clause).
|
||||
*/
|
||||
public void checkAggregation() {
|
||||
aggregationJoin();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the property to join for aggregation.
|
||||
*/
|
||||
private String aggregationJoin() {
|
||||
if (!allProperties) {
|
||||
for (STreeProperty beanProperty : propsList) {
|
||||
if (beanProperty.isAggregation()) {
|
||||
aggregation = true;
|
||||
aggregationPath = beanProperty.getElPrefix();
|
||||
return aggregationPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return aggregationPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if a top level aggregation which means the Id property must be excluded.
|
||||
*/
|
||||
public boolean isAggregationRoot() {
|
||||
boolean isAggregationRoot() {
|
||||
return aggregation && (aggregationPath == null);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user