darrachequesne / spring-data-jpa-datatables

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

How to get the specific columns in table #120

Open mzgg opened 4 years ago

mzgg commented 4 years ago

Hello, I have entity that is have many relational field. I want show to 2 column in frontend, but findAll methods is getting all columns, I don't want execute the joining their relational table

darrachequesne commented 4 years ago

Hi! I think the easiest way is to create another entity, with only necessary fields:

@Entity
public class UserSummary {
  private String id;
  private String name;
}

And to search on this entity, instead of the full User.

MariumHashmiBM commented 3 years ago

Hi! I think the easiest way is to create another entity, with only necessary fields:

@Entity
public class UserSummary {
  private String id;
  private String name;
}

And to search on this entity, instead of the full User.

Hello, this is definitely a neat approach but can you suggest in case we need the dynamic columns from query at runtime Meaning, we only know the columns at runtime to be fetched. In this case creating another entity is not an option Can we use criteriaQuery.multiselect with the rest of you plugin? Because this is very well made plugin, just need to add this specific requirement.

Thanks in advance

darrachequesne commented 3 years ago

@MariumHashmiBM That's a good question! I'm not sure that's feasible with criteriaQuery.multiselect, as it requires that the entity provides a constructor with the selected fields. Also, I don't think @Transient annotations can be added at runtime.

I'm open to suggestion on this, if you find anything.

Note: the logic for the creation of the specification is here.