darrachequesne / spring-data-jpa-datatables

Spring Data JPA extension to work with the great jQuery plugin DataTables (https://datatables.net/)
Apache License 2.0
447 stars 173 forks source link

Lazy fields are not loaded, if field is declared non-searchable #83

Closed RomanKurbanov closed 1 year ago

RomanKurbanov commented 6 years ago

If we declare a field as non-searchable:

{
            data: 'office.city',
            render: function (data) { return data ? data : '-'; },
            searchable: false
}

Then lazy-loaded field (office) won't be initialised

darrachequesne commented 1 year ago

For future readers:

The problem is that we cannot know why the field is marked as non-searchable, it might be a rendered column. You can manually fetch lazy fields with an additional specification though:

@RestController
public class MyController {

  @RequestMapping(value = "/entities", method = RequestMethod.GET)
  public DataTablesOutput<MyEntity> list(@Valid DataTablesInput input) {
    return myRepository.findAll(input, (root, query, criteriaBuilder) -> {
      if (query.getResultType() != Long.class) {
        root.fetch("relatedEntity", JoinType.LEFT);
      }
      return null;
    });
  }
}

Reference: https://github.com/darrachequesne/spring-data-jpa-datatables#fetch-lazy-fields