Filtering database rows with spring-data-jpa and spring-mvc

For starters you should stop using @RequestParam and put all your search fields in an object (maybe reuse the Travel object for that). Then you have 2 options which you could use to dynamically build a query Use the JpaSpecificationExecutor and write a Specification Use the QueryDslPredicateExecutor and use QueryDSL to write a predicate. Using … Read more

How to use Spring managed Hibernate interceptors in Spring Boot?

There’s not a particularly easy way to add a Hibernate interceptor that is also a Spring Bean but you can easily add an interceptor if it’s managed entirely by Hibernate. To do that add the following to your application.properties: spring.jpa.properties.hibernate.ejb.interceptor=my.package.MyInterceptorClassName If you need the Interceptor to also be a bean you can create your own … Read more

Spring Data JPA Update @Query not updating?

The EntityManager doesn’t flush change automatically by default. You should use the following option with your statement of query: @Modifying(clearAutomatically = true) @Query(“update RssFeedEntry feedEntry set feedEntry.read =:isRead where feedEntry.id =:entryId”) void markEntryAsRead(@Param(“entryId”) Long rssFeedEntryId, @Param(“isRead”) boolean isRead);

What’s the difference between Spring Data’s MongoTemplate and MongoRepository?

“Convenient” and “powerful to use” are contradicting goals to some degree. Repositories are by far more convenient than templates but the latter of course give you more fine-grained control over what to execute. As the repository programming model is available for multiple Spring Data modules, you’ll find more in-depth documentation for it in the general … Read more

What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?

JpaRepository extends PagingAndSortingRepository which in turn extends CrudRepository. Their main functions are: CrudRepository mainly provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sorting records. JpaRepository provides some JPA-related methods such as flushing the persistence context and deleting records in a batch. Because of the inheritance mentioned above, JpaRepository will have all the functions … Read more