Accessing the values โโof passed parameter objects using SpEL generally works like a charm, and even your syntax seems to be correct.
Maybe compare it again with Spring JPA and SpEL data
, ? ? , .
:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class MyEntity {
@Id
@GeneratedValue
private Long id;
private String title;
public MyEntity(String title) {
this.title = title;
}
public Long getId() {
return id;
}
public String getTitle() {
return title;
}
}
:
package com.example.repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import com.example.model.MyEntity;
import com.example.model.MyFilter;
public interface MyEntityRepository extends JpaRepository<MyEntity, Long> {
@Query("SELECT e FROM MyEntity e WHERE e.title = :#{#filter.title}")
Page<MyEntity> list1(@Param("filter") MyFilter filter, Pageable pageable);
}
"":
package com.example.model;
public class MyFilter {
private String title;
public MyFilter(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
}
:
package com.example.repository;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import com.example.model.MyEntity;
import com.example.model.MyFilter;
@RunWith(SpringRunner.class)
@Transactional
@SpringBootTest
public class MyEntityRepositoryTests {
@Autowired
private MyEntityRepository myEntityRepository;
@Test
public void insertAndReceiveEntityBySpEL() {
final String titleA = "A";
final String titleB = "B";
final MyEntity entityA = new MyEntity(titleA);
final MyEntity entityB = new MyEntity(titleB);
final MyEntity entityB2 = new MyEntity(titleB);
myEntityRepository.save(entityA);
myEntityRepository.save(entityB);
myEntityRepository.save(entityB2);
final MyFilter filterA = new MyFilter(titleA);
final MyFilter filterB = new MyFilter(titleB);
assertThat("Expected one hit for value A!", myEntityRepository.list1(filterA, new PageRequest(0, 5)).getContent().size(), is(1));
assertThat("Expected two hits for value B!", myEntityRepository.list1(filterB, new PageRequest(0, 5)).getContent().size(), is(2));
}
}
, , . SpEL .
, , , :
titleFilter() idFilter() ? , .
/ title MyFilter ? ? , , getter, JPA- ?
JpaSpecificationExecutor?
. , Specifications? Predicate "".