I am having trouble getting SPEL and Spring data jpa to work
Following is My Repository
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);
}
Filter Component
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
};
}
}
Following is MyEntity
package eg.domain;
@Entity
public class MyEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "title")
private String title;
......
}
Main Class
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());
}
Following is the error that I am getting
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}'!
I had the same issue when I missed "addtional" # character inside the query curly braces, so in your case you would have:
@Query("SELECT e FROM eg.domain.MyEntity e WHERE e.title = ?#{filter.title}"
but should have
@Query("SELECT e FROM eg.domain.MyEntity e WHERE e.title = ?#{#filter.title}"
Notice: ?#{filter.title}
instead of ?#{#filter.title}"
This does not match exactly the code you pasted, but might help others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With