Skip to content
Permalink
Browse files Browse the repository at this point in the history
use conditions instead of criteria as workaround
closes #18269
  • Loading branch information
atomfrede committed Apr 5, 2022
1 parent eae21e4 commit c220a21
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 35 deletions.
Expand Up @@ -50,6 +50,9 @@ import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.r2dbc.repository.support.SimpleR2dbcRepository;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.Comparison;
import org.springframework.data.relational.core.sql.Condition;
import org.springframework.data.relational.core.sql.Conditions;
import org.springframework.data.relational.core.sql.Expression;
import org.springframework.data.relational.core.sql.Select;
import org.springframework.data.relational.core.sql.SelectBuilder.SelectFromAndJoin<% if (reactiveEagerRelations.length > 0) { %>Condition<% } %>;
Expand Down Expand Up @@ -121,15 +124,10 @@ _%>

@Override
public Flux<<%= persistClass %>> findAllBy(Pageable pageable) {
return findAllBy(pageable, null);
return createQuery(pageable, null).all();
}

@Override
public Flux<<%= persistClass %>> findAllBy(Pageable pageable, Criteria criteria) {
return createQuery(pageable, criteria).all();
}

RowsFetchSpec<<%= persistClass %>> createQuery(Pageable pageable, Criteria criteria) {
RowsFetchSpec<<%= persistClass %>> createQuery(Pageable pageable, Condition whereClause) {
List<Expression> columns = <%= entityClass %>SqlHelper.getColumns(entityTable, EntityManager.ENTITY_ALIAS);
<%_ reactiveEagerRelations.forEach(function(rel) { _%>
columns.addAll(<%= rel.otherEntityNameCapitalized %>SqlHelper.getColumns(<%= rel.relationshipName %>Table, "<%= rel.relationshipName %>"));
Expand All @@ -138,19 +136,20 @@ _%>
<%_ reactiveEagerRelations.forEach(function(rel) { _%>
.leftOuterJoin(<%= rel.relationshipName %>Table).on(Column.create("<%= rel.joinColumnNames[0] %>", entityTable)).equals(Column.create("<%= rel.otherEntity.primaryKey.fields[0].columnName %>", <%= rel.relationshipName %>Table ))
<%_ }); _%>;

String select = entityManager.createSelect(selectFrom, <%= persistClass %>.class, pageable, criteria);
// we do not support Criteria here for now as of https://github.com/jhipster/generator-jhipster/issues/18269
String select = entityManager.createSelect(selectFrom, <%= persistClass %>.class, pageable, whereClause);
return db.sql(select).map(this::process);
}

@Override
public Flux<<%= persistClass %>> findAll() {
return findAllBy(null, null);
return findAllBy(null);
}

@Override
public Mono<<%= persistClass %>> findById(<%= primaryKey.type %> id) {
return createQuery(null, where(EntityManager.ENTITY_ALIAS + ".<%= primaryKey.fields[0].columnName %>").is(id)).one();
Comparison whereClause = Conditions.isEqual(entityTable.column("<%= primaryKey.fields[0].columnName %>"), Conditions.just(id.toString()));
return createQuery(null, whereClause).one();
}

<%_ if (implementsEagerLoadApis) { _%>
Expand Down
Expand Up @@ -144,8 +144,8 @@ interface <%= entityClass %>RepositoryInternal {
Flux<<%= persistClass %>> findAll();

Mono<<%= persistClass %>> findById(<%= primaryKey.type %> id);

Flux<<%= persistClass %>> findAllBy(Pageable pageable, Criteria criteria);
// this is not supported at the moment because of https://github.com/jhipster/generator-jhipster/issues/18269
// Flux<<%= persistClass %>> findAllBy(Pageable pageable, Criteria criteria);

<%_ if (implementsEagerLoadApis) { _%>

Expand Down
Expand Up @@ -33,6 +33,7 @@ import org.springframework.data.r2dbc.core.StatementMapper;
import org.springframework.data.r2dbc.mapping.OutboundRow;
import org.springframework.data.r2dbc.query.UpdateMapper;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.sql.Condition;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.sql.Conditions;
import org.springframework.data.relational.core.sql.OrderByField;
Expand Down Expand Up @@ -93,19 +94,11 @@ public class EntityManager {
* @param pageable page parameter, or null, if everything needs to be returned
* @return sql select statement
*/
public String createSelect(SelectFromAndJoin selectFrom, Class<?> entityType, Pageable pageable, Criteria criteria) {
public String createSelect(SelectFromAndJoin selectFrom, Class<?> entityType, Pageable pageable) {
if (pageable != null) {
if (criteria != null) {
return createSelectImpl(selectFrom.limitOffset(pageable.getPageSize(), pageable.getOffset()).where(Conditions.just(criteria.toString())), entityType, pageable.getSort());
} else {
return createSelectImpl(selectFrom.limitOffset(pageable.getPageSize(), pageable.getOffset()), entityType, pageable.getSort());
}
return createSelectImpl(selectFrom.limitOffset(pageable.getPageSize(), pageable.getOffset()), entityType, pageable.getSort());
} else {
if (criteria != null) {
return createSelectImpl(selectFrom.where(Conditions.just(criteria.toString())), entityType, null);
} else {
return createSelectImpl(selectFrom, entityType, null);
}
return createSelectImpl(selectFrom, entityType, null);
}
}

Expand All @@ -116,19 +109,19 @@ public class EntityManager {
* @param pageable page parameter, or null, if everything needs to be returned
* @return sql select statement
*/
public String createSelect(SelectFromAndJoinCondition selectFrom, Class<?> entityType, Pageable pageable, Criteria criteria) {
public String createSelect(SelectFromAndJoinCondition selectFrom, Class<?> entityType, Pageable pageable, Condition where) {
if (pageable != null) {
if (criteria != null) {
return createSelectImpl(selectFrom.limitOffset(pageable.getPageSize(), pageable.getOffset()).where(Conditions.just(criteria.toString())), entityType, pageable.getSort());
} else {
return createSelectImpl(selectFrom.limitOffset(pageable.getPageSize(), pageable.getOffset()), entityType, pageable.getSort());
}
return createSelectImpl(selectFrom.limitOffset(pageable.getPageSize(), pageable.getOffset()), entityType, pageable.getSort());
} else {
if (criteria != null) {
return createSelectImpl(selectFrom.where(Conditions.just(criteria.toString())), entityType, null);
} else {
return createSelectImpl(selectFrom, entityType, null);
}
return createSelectImpl(selectFrom.where(where), entityType, null);
}
}

public String createSelect(SelectFromAndJoin selectFrom, Class<?> entityType, Pageable pageable, Condition where) {
if (pageable != null) {
return createSelectImpl(selectFrom.limitOffset(pageable.getPageSize(), pageable.getOffset()).where(where), entityType, pageable.getSort());
} else {
return createSelectImpl(selectFrom.where(where), entityType, null);
}
}

Expand Down

0 comments on commit c220a21

Please sign in to comment.