Announcement Announcement Module
Collapse
No announcement yet.
JdbcTemplate and JpaRepository: Why do queries return List, not Collection or Set? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JdbcTemplate and JpaRepository: Why do queries return List, not Collection or Set?

    What is the rationale behind JdbcTemplate and Spring Data's JpaRepository queries to return List<T> instead of the more general Collection<T>?

    From a set-theoretic view, with many database tables featuring a primary, unique key, for my understanding it would make more sense to actually return a Set<T>.

    Also, to the API user, "List<T>" communicates that there is some sort of positional ordering of elements contained within that list. But unless an "ORDER BY" clause is given, ordering is actually unspecified.

    I'm curious for your answers.

  • #2
    Because that i what the underlying technology returns... Jpa returns either a single element or a list... Changing that would/could impact the behavior and from the practice of least suprise simply pass on what is returned by the framework (at least that is for Jpa).

    For JdbcTemplate there are also other methods with different return types (like a RowSet) so it actually depends on what you want, the List returning methods are just for convenience. Also you have to consider these methods exist already for 10+ years so changing the API isn't easy.

    Comment

    Working...
    X