Make the where clause variable in Spring Data JPA

Purpose

--Display search results on the list search screen --For the search condition, use the entered item, ignore it in the case of input, and narrow down the where clause

Premise

procedure

Change repository

--Add to inherit *** JpaSpecificationExecutor *** in the Repository interface to be searched

testRepository


@Repository
public interface TestRepository extends JpaRepository<TestEntity, Integer>, JpaSpecificationExecutor<TestEntity> {

}

-(Supplement) Repository is a mechanism for managing Entity in a list

Specification class preparation

--Create search conditions --This method will be used later as a search condition --Specification class plays the role of search condition used in where clause

testSpecifications


/**
	 *If the matter category exists in the search condition, add it to the where condition
	 * @param anken_kbn
	 * @return
	 */
	public Specification<TestEntity> ankenKbnContains(String anken_kbn) {
		if (Util.stringIsEmpty(anken_kbn))
			return null;
		else
			return new Specification<TestEntity>() {
				@Override
				public Predicate toPredicate(Root<TestEntity> root, CriteriaQuery<?> query,
						CriteriaBuilder cb) {
					return cb.like(root.get("anken_kbn"), "%" + anken_kbn + "%");
				}
			};
	}

--This part creates a partial match search with the item anken_kbn --Prepare methods for the conditions used by chaining with and and or ――Here, there are others

Search using Specification in Dao

--Search conditions can be set in the method chain --At this time, the repository mentioned earlier is required, so define this method where it can be injected, or in this case, take the repository object injected into the controller as the first argument and use it. ing --The second argument Form holds the value passed from the screen.

testDao


@Override
	public List<TestEntity> findBySearchItem(TestRepository testRepository,TestForm form) {
		TestSpecifications spec = new TestSpecifications();
		List<TestEntity> result = TestRepository.findAll(Specifications.where(spec.ankenKbnContains(form.getAnken_kbn()))
				.and(spec.ankenNameContains(form.getAnken_name()))
				.and(spec.reaseCampanyNameContains(form.getRease_campany_name()))
				);
		System.out.println("search results");
		System.out.println(result);

		return result;
	}

--Pass the object to be the search condition to the parameter of findAll method provided by repository interface.

Specifications.where(spec.ankenKbnContains(form.getAnken_kbn()))
				.and(spec.ankenNameContains(form.getAnken_name()))
				.and(spec.reaseCampanyNameContains(form.getRease_campany_name()))

--As mentioned above, chain search conditions with and conditions, etc. --spec is an object of specifications and uses the ankenKbnContains method created in that class. --The next form shows the value received from the screen. It becomes the character string of the search condition

Use from Controler

--In testListForm, put the search result as a list to be returned to the screen. --testDao is injected --testRepository is injected --Use Dao. I just needed an instance of the repository class in Dao, so I'm passing testRepository

testControleller


	@RequestMapping("/search")
	public ModelAndView search(@ModelAttribute("testListForm") TestListForm contractListForm,
			ModelAndView mav) {

		mav.setViewName("index");
		Iterable<TestEntity> testEntityList = testDao.findBySearchItem(testRepository, testListForm);
		
		System.out.println("search results");
		System.out.println(tstEntityList);
		
		mav.addObject("contractList", hdrEntityList);
		mav.addObject("contractListF", new TestListForm());
		return mav;

	}

that's all

--Created a variable where clause by groping ――Is this a good way to write? There may be a better way to write that I don't have.

Recommended Posts

Make the where clause variable in Spring Data JPA
Exists using Specification in Spring Data JPA
Until the use of Spring Data and JPA Part 2
Until the use of Spring Data and JPA Part 1
Check the behavior of getOne, findById, and query methods in Spring Boot + Spring Data JPA
[spring] Let's use Spring Data JPA
[Spring Data JPA] Can And condition be used in the automatically implemented method of delete?
Spring Data JPA: Write a query in Pure SQL in @Query of Repository
[How to install Spring Data Jpa]
[Spring Data JPA] Custom ID is assigned in a unique sequence at the time of registration.
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
See the behavior of entity update with Spring Boot + Spring Data JPA
Spring Data JPA SQL log output
Procedure to make the value of the property file visible in Spring Boot
Spring Autowired is written in the constructor
OR search with Spring Data Jpa Specification
Null support cache in Spring Data Redis
To write Response data directly in Spring
Jackson is unable to JSON serialize hibernateLazyInitializer in Spring Data JPA with error
Sample code for DB control by declarative transaction in Spring Boot + Spring Data JPA
How to make a unique combination of data in the rails intermediate table
[Order method] Set the order of data in Rails
Spring Data JPA save select-insert is only insert
Spring Data JPA Entity cross-reference and its notes
Organized memo in the head (Java --Data type)
Sort by Spring Data JPA (with compound key sort)
Creating a common repository with Spring Data JPA
How to define multiple orm.xml in Spring4, JPA2.1
View the Gradle task in the Spring Boot project
Spring Boot + Spring Data JPA About multiple table joins