8

Let say i have model called Employee with 70 column. How can i implement query SELECT id from t_employee in spring + querydsl without modifying lot of code from this code.

BooleanExpression paramEmployee = qEmployee.company.id.eq(new Long(data.get("company").toString()));
Iterable<Employee> employeeReportIterable =employeeRepository.findAll(paramEmployee);
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78

4 Answers4

9

If You want to use QueryDSL predicate and have single attribute response you could use just QueryDSL directly without using spring-data.

// where entityManager is a JPA EntityManager
JPAQuery<?> query = new JPAQueryFactory(entityManager);

BooleanExpression paramEmployee = qEmployee.company.id.eq(new Long(data.get("company").toString()));

List<Long> id = query.select(qEmployee.id).from(qEmployee).where(paramEmployee).fetch();
ikettu
  • 1,203
  • 12
  • 17
2

I'm afraid querydsl integration with spring only allows for construction of dynamic predicates and not full queries as indicated per spring data documentation.

I suppose though you could just use the getId() method from your Employee class without modifying the code.

megalucio
  • 5,051
  • 2
  • 22
  • 26
  • yup but that mean i need to iterate over the list with full model field right ! and split only the id to arraylist(double process) is there any way to return only the id not entire the model field T-T – Acep Muhamad Saepuloh Apr 27 '17 at 10:54
  • Not that I know of, at least using querydsl, which to be honest I'm not sure it is the best choice given the complexity of your query. Perhaps you could look into creating a custom query with @Query annotation. – megalucio Apr 27 '17 at 14:26
  • 1
    I would also like to know how to do that, because even if I use Query annotation like @megalucio said, I would lose the ability to filter with predicate. – Fernando May 22 '17 at 15:42
1

Can't you create specific method in your repository that do the work?

employeeRepository.findAllByComapanyId(new Long(data.get("company").toString()));

and implement it whith QueryDSL(depends on version):

return new JPAQuery(entityManager)
.from(employee)
.where(employee.companyId.eq(comapanyId))
.list(employee.id);
Vari
  • 21
  • 4
0

Sorry for posting on an older post, but this solution might make sense if you do not desire too much flexibility.

I would try it from another approach: create a new view in the backend SQL (or NOSQL d.b.) This view would look like this in MySql:

    CREATE OR REPLACE VIEW `employee_public` AS
        SELECT e.id as employee_id, (CASE WHEN u.publicizeLocation=false THEN null ELSE 
    e.addressLocation as addressLocation) FROM employee_id e;

Then add a new entity/dto/dao however you way you look at it in your Spring project, but also add @Immutable in addition to @Entity to the class.

This approach does not make you do anything drastic to your existing code, and it makes more sense because you can still use QuerydslPredicateExecutor or the QueryDSL builders. You type way less code, and you reduce bandwidth as you wanted.

morphytronx
  • 113
  • 3