Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix QueryBuilder chained join #913

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class QueryBuilder<T> {
private StringBuilder orderBuilder;

private final List<Object> values;
private final List<Join<T, ?>> joins;
private final List<Join<?, ?>> joins;
private final AbstractDao<T, ?> dao;
private final String tablePrefix;

Expand All @@ -81,7 +81,7 @@ protected QueryBuilder(AbstractDao<T, ?> dao, String tablePrefix) {
this.dao = dao;
this.tablePrefix = tablePrefix;
values = new ArrayList<Object>();
joins = new ArrayList<Join<T, ?>>();
joins = new ArrayList<Join<?, ?>>();
whereCollector = new WhereCollector<T>(dao, tablePrefix);
stringOrderCollation = " COLLATE NOCASE";
}
Expand Down Expand Up @@ -178,7 +178,7 @@ public <J> Join<T, J> join(Class<J> destinationEntityClass, Property destination
public <J> Join<T, J> join(Property sourceProperty, Class<J> destinationEntityClass) {
AbstractDao<J, ?> destinationDao = (AbstractDao<J, ?>) dao.getSession().getDao(destinationEntityClass);
Property destinationProperty = destinationDao.getPkProperty();
return addJoin(tablePrefix, sourceProperty, destinationDao, destinationProperty);
return (Join<T, J>) addJoin(tablePrefix, sourceProperty, destinationDao, destinationProperty);
}

/**
Expand All @@ -187,7 +187,7 @@ public <J> Join<T, J> join(Property sourceProperty, Class<J> destinationEntityCl
*/
public <J> Join<T, J> join(Property sourceProperty, Class<J> destinationEntityClass, Property destinationProperty) {
AbstractDao<J, ?> destinationDao = (AbstractDao<J, ?>) dao.getSession().getDao(destinationEntityClass);
return addJoin(tablePrefix, sourceProperty, destinationDao, destinationProperty);
return (Join<T, J>) addJoin(tablePrefix, sourceProperty, destinationDao, destinationProperty);
}

/**
Expand All @@ -196,16 +196,16 @@ public <J> Join<T, J> join(Property sourceProperty, Class<J> destinationEntityCl
* as the source for the new join to add. In this way, it is possible to compose complex "join of joins" across
* several entities if required.
*/
public <J> Join<T, J> join(Join<?, T> sourceJoin, Property sourceProperty, Class<J> destinationEntityClass,
public <J, K> Join<J, K> join(Join<?, J> sourceJoin, Property sourceProperty, Class<K> destinationEntityClass,
Property destinationProperty) {
AbstractDao<J, ?> destinationDao = (AbstractDao<J, ?>) dao.getSession().getDao(destinationEntityClass);
return addJoin(sourceJoin.tablePrefix, sourceProperty, destinationDao, destinationProperty);
AbstractDao<K, ?> destinationDao = (AbstractDao<K, ?>) dao.getSession().getDao(destinationEntityClass);
return (Join<J, K>) addJoin(sourceJoin.tablePrefix, sourceProperty, destinationDao, destinationProperty);
}

private <J> Join<T, J> addJoin(String sourceTablePrefix, Property sourceProperty, AbstractDao<J, ?> destinationDao,
private <J> Join<?, J> addJoin(String sourceTablePrefix, Property sourceProperty, AbstractDao<J, ?> destinationDao,
Property destinationProperty) {
String joinTablePrefix = "J" + (joins.size() + 1);
Join<T, J> join = new Join<T, J>(sourceTablePrefix, sourceProperty, destinationDao, destinationProperty,
Join<?, J> join = new Join<>(sourceTablePrefix, sourceProperty, destinationDao, destinationProperty,
joinTablePrefix);
joins.add(join);
return join;
Expand Down Expand Up @@ -391,7 +391,7 @@ private void checkLog(String sql) {

private void appendJoinsAndWheres(StringBuilder builder, String tablePrefixOrNull) {
values.clear();
for (Join<T, ?> join : joins) {
for (Join<?, ?> join : joins) {
builder.append(" JOIN ");
builder.append('"').append(join.daoDestination.getTablename()).append('"').append(' ');
builder.append(join.tablePrefix).append(" ON ");
Expand All @@ -403,7 +403,7 @@ private void appendJoinsAndWheres(StringBuilder builder, String tablePrefixOrNul
builder.append(" WHERE ");
whereCollector.appendWhereClause(builder, tablePrefixOrNull, values);
}
for (Join<T, ?> join : joins) {
for (Join<?, ?> join : joins) {
if (!join.whereCollector.isEmpty()) {
if (!whereAppended) {
builder.append(" WHERE ");
Expand Down