Spring Data Jpa specification implementation of given problem

Spring Data Jpa specification implementation of given problem



I am having a table like


ColA ColB
Name1 1
Name1 2
Name2 1
Name2 3
Name2 5



I want to fetch my result set as


ColA ColB
Name1 1
Name2 1



How can we implement it in spring data JPA specification ?






Why does ColB mean in your expected output?

– sp00m
Sep 10 '18 at 9:40







I think you have to describe your case more detailed. I mean, e.g tell us more about which values you want to pass to query to achive this result and other helpful information which we allow us to answer your question more precisely.

– Kamil W.
Sep 10 '18 at 9:42






I need not pass any values into it. I just want to print min value of ColB corresponding to a Name. Ex: Name1 has a min value of 1, Name2 has a min value of 1

– deepak asai
Sep 10 '18 at 9:49






yes min function in available in Specification

– Narayan Yerrabachu
Sep 10 '18 at 11:40






It's impossible in Spring Data JPA using Specification to achieve what you want because Specification affects only the WHERE clause of the SQL statement whereas you need SQL GROUB BY and MIN functionality.

– Robert Niestroj
Sep 10 '18 at 13:11




1 Answer
1



Add below code in your repository interface. I think it should work.


@Async
@Query(value = "select * " +
" from TableA ta " +
"where ta.colb = :colb",nativeQuery=true)
List<T> findByColA(@Param("colb") String colb);



or



with specification



Create Specification Object:



Criteria functions(SUM, MAX or MIN) for Specification:



javax.persistence.criteria.CriteriaQuery<T> Check this interface to find more information.


javax.persistence.criteria.CriteriaQuery<T>


final class UserSpecifications

private UserSpecifications()

static Specification<User> findData(String colb, String columnName)
return new Specification<User>()
public Predicate toPredicate(Root<User> root,
CriteriaQuery<?> query,
CriteriaBuilder cb)
root = query.from(User.class);
Predicate colbp = cb.equal(root.get(columnName), colb);
Expression<String> groupByExp = root.get(columnname).as(String.class);
query.where(colbp);
query.select(cb.min(root.get(column.getName())));
query.groupBy(groupByExp);





Specification<User> spec = UserSpecifications.findData("1", "columnName");

List<User> allUsers = repository.findAll(spec);



Tutorial :Spring data Tutorial



specification example: Spring Data specification



Spring Specification example:Spring Specification How-to



Stackoverflow ref: Spring Specification






I want to use with Specification. So that i can pass findAll(specification)

– deepak asai
Sep 10 '18 at 10:03






yes u can also do it with specification List<T> findAll(Specification<T> spec);

– Narayan Yerrabachu
Sep 10 '18 at 10:06






My doubt is how to construct that Specification Object.

– deepak asai
Sep 10 '18 at 10:16






baeldung.com/…

– Narayan Yerrabachu
Sep 10 '18 at 10:35






My doubt is to how to implement Max and min functions and spring data JPA. Because i dont think so it is easy to construct Specification object for my problem. Can some clearly construct a specification object for my proble

– deepak asai
Sep 10 '18 at 11:07



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)