Spring Data – Multi-column searches

Here is sample of such Specification for User: public static Specification<User> containsTextInName(String text) { if (!text.contains(“%”)) { text = “%” + text + “%”; } String finalText = text; return (root, query, builder) -> builder.or( builder.like(root.get(“lastname”), finalText), builder.like(root.get(“firstname”), finalText) ); } or even more customizable implementation: public static Specification<User> containsTextInAttributes(String text, List<String> attributes) { if … Read more

How to store @Query sql in external file for CrudRepository?

Use below steps. Create jpa-named-queries.property file in src/main/resources–>META-INF Folder Defile your query in given properties file. Above screenshot look closely.Here Group is Entity name, while Method should match with method define in Repository interface. Query should have object name instead table name and instead of column name provide variable name given in entity for respective … Read more

JpaRepository caches newly created object. How to refresh it?

If you are using Hibernate, this is the expected result. When you call translationRepository.saveAndFlush(translation) and translationRepository.findOne(t.getId()) one after the other, they hit the same Hibernate session which maintains a cache of all objects that it has worked on. Therefore, the second call simply returns the object passed to the first. There is nothing in those … Read more

how to store PostgreSQL jsonb using SpringBoot + JPA?

Tried this but understood nothing! To fully work with jsonb in Spring Data JPA (Hibernate) project with Vlad Mihalcea’s hibernate-types lib you should just do the following: 1) Add this lib to your project: <dependency> <groupId>com.vladmihalcea</groupId> <artifactId>hibernate-types-52</artifactId> <version>2.2.2</version> </dependency> 2) Then use its types in your entities, for example: @Data @NoArgsConstructor @Entity @Table(name = “parents”) … Read more

Spring Data JPA and Querydsl to fetch subset of columns using bean/constructor projection

Looks like custom repository implementation is the way to go for now until something similar available in spring data. I have gone through http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/repositories.html#repositories.custom-implementations Here is my implementation which works. However it would be good to have this method available directly in Spring-Data-JPA Step 1: Intermediate interface for shared behavior public interface CustomQueryDslJpaRepository <T, ID … Read more

Set default page size for JPA Pageable Object

If you are talking about a Spring Data PagingAndSortingRepository you can set the default page size by using the @PageableDefault on a Controller method as follows: public String listClients(@ModelAttribute FilterForm form, Model model, WebRequest request, @PageableDefault(sort = { “surname”, “forename”, “address.town” }, value = 50) Pageable pageable) { } Or you can configure a global … Read more

Spring Data JPA: Query by Example?

This is now possible with Spring Data. Check out http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example Person person = new Person(); person.setLastname(“Smith”); Example<Person> example = Example.of(person); List<Person> results = personRepository.findAll(example); Note that this requires very recent 2016 versions <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.10.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>1.12.1.RELEASE</version> </dependency> see https://github.com/paulvi/com.example.spring.findbyexample