Home > Net >  Cannot create a method in a repository class for an Entity class with `@SqlResultSetMapping` in JPA
Cannot create a method in a repository class for an Entity class with `@SqlResultSetMapping` in JPA

Time:11-17

I created Entity and Repository for the following, but it fails to create a method findTestLikeNo with parameter ?1 set in the query.

Please let me know if you know the cause of this problem.

mport jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;

@SqlResultSetMapping(
        name="testMap",
        entities = {
                @EntityResult(
                        entityClass= TestEntity.class,
                        fields = {
                                @FieldResult(name="testNo", column="test_no"),
                                @FieldResult(name="testItem", column="test_item"),
                        }
                )
        }
)
@NamedNativeQueries({
        @NamedNativeQuery(
                name = "TestEntity.findTest",
                query = "w1.test_no, w1.test_item"  
                        "  from W_table w1",
                resultSetMapping = "testMap",
                resultClass = TestEntity.class
        ),
        @NamedNativeQuery(
                name = "TestEntity.findTestLikeNo",
                query = "w1.test_no, w1.test_item"  
                        "  from W_table w1"  
                " where w1.test_no like ?1;",
                resultSetMapping = "testMap",
                resultClass = TestEntity.class
        )
})
@Entity
@Getter
@Setter
public class TestEntity {

    public TestEntity(){
    }

    @Id
    private String testNo;

    private String testItem;
}
import hoge.mypkg.TestEntity;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface TestRepository extends JpaRepository<TestEntity, String> {
    List<TestEntity> findTest();
    List<TestEntity> findTestLikeNo(String testNo);
}
Caused by: org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'testRepository' defined in 
   TestRepository defined in @EnableJpaRepositories declared on 
     JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration:
      Could not create query for public abstract java.util.List 

TestRepository.findTestLikeNo(java.lang.String); 
  Reason: Failed to create query for method public abstract java.util.List 

TestRepository.findTestLikeNo(java.lang.String);
 No property 'findTestLikeNo' found for type 'TestEntity'

CodePudding user response:

The extra semicolon was the cause.

where w1.test_no like ?1; -> where w1.test_no like ?1

        @NamedNativeQuery(
                name = "TestEntity.findTestLikeNo",
                query = "w1.test_no, w1.test_item"  
                        "  from W_table w1"  
                " where w1.test_no like ?1",
  • Related