Spring JPA Spel data - @Query Issue

I'm having trouble getting SPEL and Spring jpa data to work

Below 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
        };
    }
}

Next - 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());
        }

Below is the error I'm 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}'!
+4
source share
3 answers
private String title = "title title1";

the filter header is private, and I could not see any getter for this property. Maybe this is a problem.

0
source

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 "".

0

I had the same problem when I missed the "extra" character # in curly brackets of the request, so in your case you will have:

@Query("SELECT e FROM eg.domain.MyEntity e     WHERE e.title = ?#{filter.title}"

but must have

@Query("SELECT e FROM eg.domain.MyEntity e     WHERE e.title = ?#{#filter.title}"

Please note: ?#{filter.title}instead?#{#filter.title}"

This doesn't exactly match the code you inserted, but may help others.

0
source

Source: https://habr.com/ru/post/1584191/


All Articles