This is what I've found so fare ... it is close :-)
It works except for the fact that the generated select does not only contain those properties configured with .select() but also the id columns, which is clear for partial objects, but here I'd like to avoid this.
So the thing (or one of the things) is, to avoid rendering the id columns in the select and where clause.
BTW: This is something you do in Hibernate using Projections, probably such a API should be the way to go for Avaje too, but I just wanted to implement this very feature I need now.
import com.avaje.ebean.expression.Expression;
import com.avaje.ebean.expression.ExpressionRequest;
import com.avaje.ebean.query.OrmQuery;
import com.avaje.ebean.server.core.DefaultServer;
import com.avaje.ebean.server.core.QueryRequest;
import com.avaje.ebean.server.query.CQuery;
import com.avaje.ebean.server.query.CQueryBuilder;
import java.util.List;
public class InQueryExpression implements Expression
{
private final String propertyName;
private final OrmQuery query;
private transient CQuery builtQuery;
public InQueryExpression(String propertyName, OrmQuery query)
{
this.propertyName = propertyName;
this.query = query;
}
public String getPropertyName()
{
return propertyName;
}
public int queryPlanHash()
{
int hc = InQueryExpression.class.getName().hashCode();
hc = hc * 31 + propertyName.hashCode();
hc = hc * 31 + query.getQueryPlanHash();
return hc;
}
public int queryBindHash()
{
return 0;
}
public void addSql(ExpressionRequest request)
{
CQuery builtQuery = getBuiltQuery(request);
request.append(" (");
request.append(propertyName);
request.append(") in (");
request.append(builtQuery.getGeneratedSql());
request.append(") ");
}
private CQuery getBuiltQuery(ExpressionRequest request)
{
if (builtQuery == null)
{
DefaultServer defaultServer = (DefaultServer) request.getQueryRequest().getEbeanServer();
QueryRequest qr = defaultServer.createQueryRequest(query, request.getQueryRequest().getTransaction());
CQueryBuilder builder = new CQueryBuilder(defaultServer.getPlugin().getPluginCore());
builtQuery = builder.buildQuery(qr, defaultServer.getPlugin().getDbConfig().getBinder());
}
return builtQuery;
}
public void addBindValues(ExpressionRequest request)
{
CQuery builtQuery = getBuiltQuery(request);
List