Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring data JPA Spel - @Query Issue

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}'!
like image 532
ArunM Avatar asked Apr 23 '15 08:04

ArunM


1 Answers

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.

like image 190
walkeros Avatar answered Oct 20 '22 00:10

walkeros