我有麻烦SPEL和Spring数据JPA工作春数据JPA的SpeI - @Query问题
以下是我的仓库
package eg.repository;
public interface MyEntityRepository extends JpaRepository<MyEntity, Long>,JpaSpecificationExecutor<MyEntity> {
@Query("SELECT e FROM eg.domain.MyEntity e " +
"WHERE e.title = :#{#filter.title}"
)
Page<MyEntity> list1(@Param("filter") MyFilter filter,Pageable pageable);
}
滤波器组件
package eg.service;
import org.springframework.stereotype.Component;
@Component("filter")
public class MyFilter {
public String titleFilter() {
return "%title%";
}
private String title = "title title1";
public Long[] idFilter() {
return new Long[] {
1L, 2L
};
}
}
以下是MyEntity
package eg.domain;
@Entity
public class MyEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "title")
private String title;
......
}
主类
LOG.info("Application initialized " + annotationConfigApplicationContext);
MyEntityRepository myEntityRepository =
(MyEntityRepository) annotationConfigApplicationContext.getBean(MyEntityRepository.class);
MyFilter filter = annotationConfigApplicationContext.getBean(MyFilter.class);
PageRequest pageRequest = new PageRequest(0, 5);
Page<MyEntity> page = myEntityRepository.list1(filter,pageRequest);
List<MyEntity> entities= page.getContent();
for(MyEntity entity: entities){
System.out.println(entity.getId() + " TITLE " + entity.getTitle());
}
下面是我得到过滤器的
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myEntityRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Using named parameters for method public abstract org.springframework.data.domain.Page eg.repository.MyEntityRepository.list1(eg.service.MyFilter,org.springframework.data.domain.Pageable) but parameter 'filter' not found in annotated query 'SELECT e FROM eg.domain.MyEntity e WHERE e.title = :#{#filter.title}'!
为什么你不只是给标题作为参数,而不是像'@Query(“SELECT e FROM eg.domain.MyEntity e WHERE e.title =:filter”)'? –
我只是想让Spel和Spring数据一起工作。一旦这个工程正在计划添加更复杂的东西,如在方法idFilter()等中可以看到的。 – ArunM
我也无法得到一个简单的#{#variable}表达式。 – RedDeckWins