spring-data-jpa-datatables icon indicating copy to clipboard operation
spring-data-jpa-datatables copied to clipboard

QueryDSL predicates with @ManyToOne relationships do not work

Open darrachequesne opened this issue 8 years ago • 3 comments

Currently, @ManyToOne relationships with QDataTablesRepository break global search:

Server:

@Entity
public class User {
  @Id private Integer id;

  @ManyToOne
  @JoinColumn(name = "id_address")
  private Address address;
}

@Entity
public class Address {
  @Id private Integer id;
}

Client:

$('table#users').DataTable({
    columns : [ {
        data : 'id'
    }, {
        data : 'address.id',
        // searchable: false,
        render: function(data, type, row) {
            return data ? data : '';
        }
    }]
});

The countQuery issued by QueryDslJpaRepository.findAll(Predicate predicate, Pageable pageable) triggers the following query:

select count(user0_.id) as col_0_0_
  from user_ user0_
  cross join address address1_
  where
    user0_.id_address=address1_.id
    and (lower(cast(user0_.id as char)) like ? escape '\' 
    or lower(cast(address1_.id as char)) like ? escape '\')

To add a proper LEFT JOIN, an (ugly) workaround would be to manually add JPQLQuery countQuery = createQuery(predicate).leftJoin(entity.get("address")). But if someone has a better idea, I'm open to suggestions!

Update: since QueryDSL generates a leftJoin instead of an innerJoin when a sort is applied on the given column (ref), the following workaround works pretty well:

var table = $('table#users').DataTable({
     orderFixed: [ <your-column-id>, 'asc' ],
});

darrachequesne avatar Mar 31 '16 00:03 darrachequesne

Any news on this one? Tested the provided workaround but with no luck :(

Dazzel avatar Jul 28 '17 14:07 Dazzel

can i work on this?

ShivanshCharak avatar Oct 01 '23 07:10 ShivanshCharak

@ShivanshCharak yeah, sure :+1:

darrachequesne avatar Oct 02 '23 19:10 darrachequesne