【问题标题】:Cannot create a method in a repository class for an Entity class with `@SqlResultSetMapping` in JPA无法在 JPA 中使用“@SqlResultSetMapping”在存储库类中为实体类创建方法
【发布时间】:2022-11-16 18:09:44
【问题描述】:

我为以下内容创建了实体和存储库,但它无法创建方法 findTestLikeNo,并在查询中设置了参数 ?1

如果您知道此问题的原因,请告诉我。

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'

【问题讨论】:

  • 方法 findTestLikeNo(String testNo) 的声明中不缺少 @Param 吗?

标签: java spring jpa


【解决方案1】:

额外的分号是原因。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 2022-01-17
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多