Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select only specific column in Spring with Querydsl?

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);
like image 652
Acep Muhamad Saepuloh Avatar asked May 20 '26 01:05

Acep Muhamad Saepuloh


2 Answers

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();
like image 194
ikettu Avatar answered May 21 '26 17:05

ikettu


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.

like image 43
megalucio Avatar answered May 21 '26 18:05

megalucio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!