Announcement Announcement Module
Collapse
No announcement yet.
How to retrieve the ids of entities using Specification Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to retrieve the ids of entities using Specification

    I am currently using a repository that I have created by extending "edu.wgu.pcv.domain.repository.JpaRepository" and enabling jpa repositories. I pass in a org.springframework.data.jpa.domain.Specification in order to filter data. However, I would also like a way to be able to only retrieve the entities ids as a result List instead of returning the entities. I know that I could retrieve a list of the entities and then loop through them to get all the ids, but it seems to be overkill to retrieve the whole entity when all you need is the id (a simple Long).

  • #2
    Criteria Queries is a way to solve this problem. One option to select single/multiple values is by selecting an array (Object[]).
    Another is to create a wrapper Object that the criteria builder will use in constructing the results of the query.
    Another option the Tuple criteria is a good option for your use case, this option uses the javax.persistence.Tuple interface, see links below to documentation on example code to implement these options

    Array/Wrapper:
    http://docs.jboss.org/hibernate/enti...ry-multiselect

    Tuple:
    http://docs.jboss.org/hibernate/enti...criteria-tuple

    Comment

    Working...
    X