generator-jhipster: buildSpecification returns duplicate rows when combined with ManyToMany In Criteria

Overview of the issue

When we have a ManyToMany relationship, a request to the server with groupId.in=1,2,3 returns the rows with groupIds as many time as present in the filter. ex: JDL

entity Task {
    name String required
}

entity Group {
    name String required
}

relationship ManyToMany {
    Task{group(name)} to Group{task(name)}
}

If Task 1 has groups: 1,2,3,4 and a request is sent with groupId.in=1,2,3 the line is shown 3 times as there is no distinct in the code that build the specification: io.github.jhipster.service.QueryService<>.buildSpecification.

Motivation for or Use Case

Be able to list all entities linked to other entities, in my example, all tasks in some specific groups.

Reproduce the error
Related issues
Suggest a Fix

buildSpecification ahould have “an optional parameters” and when set to true calls, query.distinct(true) A more genreic solution would be to add a parameter queryFunction that is applied to query, which would allow to set multiple orders…

JHipster Version(s)

6.1.2, not a regression

JHipster configuration
Entity configuration(s) entityName.json files generated in the .jhipster directory
Browsers and Operating System
  • Checking this box is mandatory (this is just to show you read everything)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 5
  • Comments: 16 (11 by maintainers)

Most upvoted comments

Quick solution in generated code from jhipster is possible. For generation tool don’t know. In method ObjectQueryService.createSpecification (which is generated by jhipster) replace this part of code: Specification<Object> specification = Specification.where(null); with this: Specification<Object> specification = (root, query, cb) -> { query.distinct(true); return null; }; Thanks to this will method createSpecification return Specification object with support for distinct values by default.

But it would by nice to have this implemented by default in generated code. Of course with option to turn it on / off.