Skip to main content

Posts

Showing posts from March, 2020

Spring Expression Language (SpEL) Tutorial

The Spring Expression Language (“SpEL”) is an expression language that supports querying and manipulating an object graph at runtime. In this post we'll see examples of using SpEL with both XML and annotation based configuration . The expression language supports the following functionality: Literal expressions Boolean, relational and mathematical operators Regular expressions Class expressions Accessing properties, arrays, lists, and maps Method invocation Assignment Calling constructors Bean references Inline lists Inline maps Ternary operator Table of contents SpEL expressions in Bean Definitions SpEL Relational operators SpEL Logical operators SpEL Mathematical operators SpEL Bean reference and Method invocation example Using SpEL Safe Navigation Operator (?.) SpEL Ternary and Elvis operators SpEL with regular expression SpEL expressions in Bean Definitions Syntax to define the expressio

Spring Data JPA Auditing Example

In this post we’ll see how to configure Spring Data JPA to automatically store auditing information like created by, created date, modified by, modified date for any entity. Spring Data auditing support Spring Data provides support to keep track of who created or changed an entity and the time when this happened. To use this audit functionality you need to equip your entity classes with auditing metadata that can be defined either using annotations or by implementing an interface. Annotations that are used are as following- @CreatedBy - To capture the user who created the entity. @LastModifiedBy - To capture the user who modified the entity. @CreatedDate - To capture the point in time when entity is created. @LastModifiedDate - To capture the point in time when entity was last modified. If you want to see how to create Maven project, please check this post- Create Java Project Using Maven in Eclipse For setting up a Spring Data JPA project and the req

Spring Data JPA Pagination and Sorting Example

In this Spring Data JPA pagination and sorting example we’ll see how to use PagingAndSortingRepository to paginate access by providing number of records per page and page number. You can sort records too by passing the field (or group of fields) on which sorting is done along with page properties or separately. We’ll create a rest web service using Spring Web MVC, JPA implementation used is Hibernate and DB is MySQL. For maven dependencies required for the example and configuration classes for setting up EntityManagerFactory and web application you can refer this post- Spring Data JPA @Query Annotation Example Table of contents DB table Query JPA Entity class Spring Data JPA Repository Rest Controller Spring Data JPA example – Service class Paging and Sorting options Page Vs Slice Deploying the Spring Data JPA application DB table Query MySQL DB table used for this Spring data JPA can be created using the following query. CREATE TABLE `empl

Spring Data JPA @Query Annotation Example

In the Spring Data JPA Example we have already seen an integrated example of Spring Rest Controller + JPA (Hibernate) + MySQL. In that example query look up strategy for user defined query was automatic where Spring framework did the work of deriving query by parsing the method name. Though getting a query derived from the method name is quite convenient but you may face the situation where method name parser does not support the keyword you used or the method name would get unnecessarily ugly. In that case Spring Data JPA provides two options- You can use JPA named queries using @NamedQuery annotation. Annotate your query method with @Query . In this article we'll see Spring data JPA example with @Query annotation. For Spring Data JPA example using @NamedQuery annotation check this post- Spring Data JPA @NamedQuery Annotation Example Table of contents Spring Data JPA with @Query annotation Maven dependencies DB table Query Spring data JPA ex

Spring Data JPA @NamedQuery Annotation Example

In the Spring Data JPA Example we have already seen an integrated example of Spring MVC + JPA (Hibernate) + MySQL. In that example, query look up strategy for user defined query was automatic where Spring framework did the work of deriving query by parsing the method name. Though getting a query derived from the method name is quite convenient but you may face the situation where method name parser does not support the keyword you used or the method name would get unnecessarily ugly. In that case you can use JPA named queries using @NamedQuery annotation or annotate your query method with @Query . In this article we'll see Spring data JPA example with @NamedQuery annotation. For Spring Data JPA example using Query annotation check this post- @ Spring Data JPA @Query Annotation Example Table of contents Spring Data JPA with named query Maven dependencies DB table Query JPA Entity – Spring data JPA Spring Data JPA Repository Spring Data JPA examp