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

Add distinct query for entity filtering #15929

Merged
merged 1 commit into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
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 @@ -125,6 +125,10 @@ public class <%= serviceClassName %> extends QueryService<<%= persistClass %>> {
protected Specification<<%= persistClass %>> createSpecification(<%= criteria %> criteria) {
Specification<<%= persistClass %>> specification = Specification.where(null);
if (criteria != null) {
// This has to be called first, because the distinct method returns null
if (criteria.getDistinct() != null) {
specification = specification.and(distinct(criteria.getDistinct()));
}
if (criteria.get<%= primaryKey.nameCapitalized %>() != null) {
specification = specification.and(<%= getSpecificationBuilder(primaryKey.type) %>(criteria.get<%= primaryKey.nameCapitalized %>(), <%= persistClass %>_.<%= primaryKey.name %>));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,16 @@ public class <%= entityClass %>Criteria implements Serializable, Criteria {
private <%- filterVariable.filterType %> <%= filterVariable.name %>;
<%_ }); _%>

private Boolean distinct;

public <%= entityClass %>Criteria() {
}

public <%= entityClass %>Criteria(<%= entityClass %>Criteria other) {
<%_ filterVariables.forEach((filterVariable) => { _%>
this.<%= filterVariable.name %> = other.<%= filterVariable.name %> == null ? null : other.<%= filterVariable.name %>.copy();
<%_ }); _%>
this.distinct = other.distinct;
}

@Override
Expand All @@ -137,6 +140,14 @@ public class <%= entityClass %>Criteria implements Serializable, Criteria {

<%_ }); _%>

public Boolean getDistinct() {
return distinct;
}

public void setDistinct(Boolean distinct) {
this.distinct = distinct;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -148,25 +159,18 @@ public class <%= entityClass %>Criteria implements Serializable, Criteria {
final <%= entityClass %>Criteria that = (<%= entityClass %>Criteria) o;
return
<%_ filterVariables.forEach((filterVariable, index) => { _%>
<%_ if (filterVariables.length === index + 1) { _%>
Objects.equals(<%= filterVariable.name %>, that.<%= filterVariable.name %>);
<%_ } else { _%>
Objects.equals(<%= filterVariable.name %>, that.<%= filterVariable.name %>) &&
<%_ } _%>
<%_ }); _%>
Objects.equals(distinct, that.distinct);
}

@Override
public int hashCode() {
return Objects.hash(
<%_ filterVariables.forEach((filterVariable, index) => { _%>
<%_ if (filterVariables.length === (index + 1)) { _%>
<%= filterVariable.name %>
<%_ } else { _%>
<%= filterVariable.name %>,
<%_ } _%>
<%_ }); _%>
);
distinct);
}

// prettier-ignore
Expand All @@ -176,6 +180,7 @@ public class <%= entityClass %>Criteria implements Serializable, Criteria {
<%_ filterVariables.forEach((field) => { _%>
(<%= field.name %> != null ? "<%= field.name %>=" + <%= field.name %> + ", " : "") +
<%_ }); _%>
(distinct != null ? "distinct=" + distinct + ", " : "") +
"}";
}

Expand Down