【问题标题】:How to provide final value for @Query(value=xyz) in spring-boot application如何在 spring-boot 应用程序中为 @Query(value=xyz) 提供最终值
【发布时间】:2021-04-13 01:35:18
【问题描述】:

我正在从属性文件中读取我的 SQL 查询,并且我正在传递给 @Query(value=xyz) 但得到一致的错误“属性值必须是常量”。有没有可用的解决方案?

public class QueryUtils {

    public static Properties properties;

    static {
        try {
            FileReader reader = new FileReader("sql.properties");
            properties = new Properties();
            properties.load(reader);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static  String findAllTags2 = properties.getProperty("ResourceTagRepository.find_all_tags");
}

我正在尝试在打击代码中使用它。

@Component
@Repository
public interface QueryRepo extends JpaRepository<XYZ, Long>, JpaSpecificationExecutor<XYZ> {


    @Query(value = findAllTags2)
    public List<ResourceTagMapping> findAllTags(@Param("list") Set<String> names);
}

【问题讨论】:

    标签: java sql spring spring-boot hibernate


    【解决方案1】:

    尝试让您的 String findAllTags2 成为最终字段

    【讨论】:

    • 否 它不工作。我也尝试了一个示例代码但没有工作,在@query code ->“属性值必须是常量”@Query(value = RpasQueryUtils.Q1.getval()) public List findAllTags( @Param("list") Set canonicalNames);公共枚举 RpasQueryUtils { Q1("select * from xyz"), RpasQueryUtils(String query) { this.query = query; } 私有最终字符串查询; public String getValue() { 返回查询; } public String getval() { 返回查询; } }
    猜你喜欢
    • 2020-10-31
    • 1970-01-01
    • 2018-02-24
    • 2016-02-11
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 2020-10-06
    相关资源
    最近更新 更多