Creating Spring Boot MVC application with AWS DynamoDB in 10 mins

AWS DynamoDB DB is a serverless NOSQL database. You can understand how to build a spring boot Java web MVC application (Game Leaderboard) reading a AWS DynamoDB in 10 mins. Source of the demo code: Command to run the project: mvn spring-boot:run Video explain the table design: Prerequisite: Install the AWS CLI: Video explain the how to create the table:

Adding Hibernate Entity Level Filtering feature to Spring Data JPA Repository

Those who have used data filtering features of hibernate should know that it is very powerful. You could define a set of filtering criteria to an entity class or a collection. Spring data JPA is a very handy library but it does not have fitering features. In this post, I will demonstrate how to add the hibernate filter features at entity level. We can just use annotation in your repositoy interface to enable this features.

Step 1. Define filter at entity level as usual. Just use hibernate @FilterDef annotation.

@Table(name = "STUDENT")
@FilterDef(name="filterBySchoolAndClass", parameters={@ParamDef(name="school", type="string"),@ParamDef(name="class", type="integer")})
public class Student extends GenericEntity implements Serializable {

Step2. Define two custom annotations.

These two annotations are to be used in your repository interfaces. You could apply the hibernate filter defined in step 1 to specific query through these annotations.

public @interface EntityFilter {
 FilterQuery[] filterQueries() default  {};

public @interface FilterQuery {
 String name()  default "";
 String jpql()  default "";

Step3. Add a method to your Spring data JPA base repository.

This method will read the annotation you defined (i.e. @FilterQuery) and apply hibernate filter to the query by just simply unwrap the EntityManager. You could specify the parameter in your hibernate filter and also the parameter in you query in this method.

If you do not know how to add custom method to your Spring data JPA base repository, please see my previous post for how to customize your Spring data JPA base repository for detail. You can see in previous post that I intentionally expose the repository interface (i.e. the springDataRepositoryInterface property) in the GenericRepositoryImpl. This small tricks enable me to access the annotation in the repository interface easily.

public List<T> doQueryWithFilter( String filterName, String filterQueryName, Map inFilterParams, Map inQueryParams){
    if (GenericRepository.class.isAssignableFrom(getSpringDataRepositoryInterface())) {
       Annotation entityFilterAnn = getSpringDataRepositoryInterface().getAnnotation(EntityFilter.class);
       if(entityFilterAnn != null){
        EntityFilter entityFilter = (EntityFilter)entityFilterAnn;
        FilterQuery[] filterQuerys  = entityFilter.filterQueries() ;
        for (FilterQuery fQuery : filterQuerys) { 
         if (StringUtils.equals(filterQueryName, {
          String jpql = fQuery.jpql();
          Filter filter = em.unwrap(Session.class).enableFilter(filterName);
          //set filter parameter
          for (Object key: inFilterParams.keySet()) {
           String filterParamName = key.toString();
           Object filterParamValue = inFilterParams.get(key);
           filter.setParameter(filterParamName, filterParamValue);
          //set query parameter
          Query query= em.createQuery(jpql);
          for (Object key: inQueryParams.keySet()) {
           String queryParamName = key.toString();
           Object queryParamValue = inQueryParams.get(key);
           query.setParameter(queryParamName, queryParamValue);
          return query.getResultList();
     return null;

Usage example:

In your repositry, define which query you would like to apply hibernate filter through your @EntityFilter and @FilterQuery annotation.

@EntityFilter (
 filterQueries = {
       jpql="SELECT s FROM Student LEFT JOIN FETCH s.Subject where s.subject = :subject" ),
       jpql="SELECT s FROM Student LEFT JOIN s.TeacherSubject where s.teacher =  :teacher")       
public interface StudentRepository extends GenericRepository<Student, Long> {

In your service or business class that inject your repository, you could just simply call the doQueryWithFilter() method to enable the filtering function.

public class StudentService {

 private StudentRepository studentRepository;

 public List<Student> searchStudent( String subject, String school, String class) {
  List<Student> studentList;

  // Prepare parameters for query filter
  HashMap<String, Object> inFilterParams = new HashMap<String, Object>();
  inFilterParams.put("school", "Hong Kong Secondary School");
  inFilterParams.put("class", "S5");

  // Prepare parameters for query
  HashMap<String, Object> inParams = new HashMap<String, Object>();
  inParams.put("subject", "Physics");

  studentList = studentRepository.doQueryWithFilter(
    "filterBySchoolAndClass", "query1",
    inFilterParams, inParams);

  return studentList;


Unknown said…
Nice blog i really like this blog.php

Popular posts from this blog

Sample Apps: Spring data MongoDB and JSF Integration tutorial (PART 1)

Writing your spring security expression language annotation - PART 1

Customizing Spring Data JPA Repository