Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

My Favorite Spring Data JPA Feature

DZone's Guide to

My Favorite Spring Data JPA Feature

I’m impressed by the complexity of query you can write using Spring Data. My favourite feature is returning the first or top records from a table.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

I like Spring Data JPA. It helps simplify my codebase, and most of the time frees me up from writing JPAQL or SQL. I’m also impressed by the complexity of query you can write using Spring Data. My favourite feature is returning the first or top records from a table.

Let's say I have a table tracking document versions -

DOCUMENT_TABLE

DOCUMENT_ID NAME VERSION
1 mydoc.doc
1
2 mydoc.doc 2
3 mydoc.doc 3

With its associated JPA object -

@Entity
@Table(name = "DOCUMENT_TABLE")
public class Document implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @Column(name = "DOCUMENT_ID")
   private Long documentId;

   @Basic(optional = false)
   @NotNull
   private String name;

   @Basic(optional = false)
   @NotNull
   private Long version;

}

My SQL would be -

SELECT *
FROM
  (SELECT *
  FROM document_table dt
  WHERE dt.name = 'mydoc.doc'
  ORDER BY dt.VERSION DESC
  )
WHERE rownum = 1;

Or in JPA -

select d
   from Document d
   where d.name = :name
   and d.version = (select max(d.version) from Document d where d.name = :name)

But I want to keep my codebase consistent so the first thing I can try is -

public interface DocumentRepository extends CrudRepository<Document, Long> {
    List<Document> findByNameOrderByVersionDesc(String name);
}

I can get the first record -

List<Document> documentList =
   documentRepository.findByNameOrderByVersionDesc("mydoc.doc");
Document document = documentList.get(0);

Another alternative is to have a straight SQL query in the repository, but the best option is to use the Top feature in spring data -

public interface DocumentRepository extends CrudRepository<Document, Long> {
    Document findFirstByNameOrderByVersionDesc(String name);
    Document findTopByNameOrderByVersionDesc(String name);
}

The above methods being equivalent

We can even use Top to return the top n records -

Document findTop2ByNameOrderByVersionDesc(String name);

Interestingly, but not suprisingly, the underlying SQL generated by Spring Data opts for the rownum construct I used in my original SQL statement

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
java ,spring ,spring data jpa ,spring data

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}