DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

A Guide to Continuous Integration and Deployment: Learn the fundamentals and understand the use of CI/CD in your apps.

Related

  • Advanced Search and Filtering API Using Spring Data and MongoDB
  • Manage Hierarchical Data in MongoDB With Spring
  • Spring Data: Data Auditing Using JaVers and MongoDB
  • CRUD Operations on Deeply Nested Comments: Scalable Spring Boot and Spring Data approach

Trending

  • Demystifying Static Mocking With Mockito
  • 7 Must-Know Object-Oriented Software Patterns (Part One)
  • Enhancing Observability With AI/ML
  • DDD and Microservices
  1. DZone
  2. Data Engineering
  3. Databases
  4. My Favorite Spring Data JPA Feature

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.

Martin Farrell user avatar by
Martin Farrell
·
Jul. 01, 16 · Opinion
Like (26)
Save
Tweet
Share
45.7K Views

Join the DZone community and get the full member experience.

Join For Free

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_IDNAMEVERSION
1mydoc.doc
1
2mydoc.doc2
3mydoc.doc3

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

Spring Data Data (computing) Spring Framework Database

Published at DZone with permission of Martin Farrell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Advanced Search and Filtering API Using Spring Data and MongoDB
  • Manage Hierarchical Data in MongoDB With Spring
  • Spring Data: Data Auditing Using JaVers and MongoDB
  • CRUD Operations on Deeply Nested Comments: Scalable Spring Boot and Spring Data approach

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: