【问题标题】:Validation failed for query for method public, spring jpa?查询方法public,spring jpa的验证失败?
【发布时间】:2020-05-30 06:37:05
【问题描述】:

我正在创建一个 api 来从数据库中查询并返回结果。

这是请求

@RequestMapping(value = "/config", params = { "appCode", "appVersion" }, method = RequestMethod.GET)
public List<AppConfig> getConfig(@RequestParam(value = "appCode", required = true) String appCode,
        @RequestParam(value = "appVersion", required = true) String appVersion) {
    return configRepository.findByCodeAndVersion(appCode, appCode);
}

表格类

@Entity
@Table(name = "app_config")
@EntityListeners(AuditingEntityListener.class)
public class AppConfig {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "serial")
private long id;
@Column(name = "app_code", nullable = false)
private String appCode;
@Column(name = "app_name", nullable = false)
private String appName;
@Column(name = "api_url", nullable = true)
private String apiUrl;
@Column(name = "db_name", nullable = true)
private String dbName;
@Column(name = "app_version", nullable = false)
private String appVersion;
}

我有自定义查询的存储库

@Repository
public interface AppConfigRepository extends CrudRepository<AppConfig, Long> {

@Query("SELECT n FROM AppConfig WHERE n.appCode = ?1 and n.appVersion = ?2")
List<AppConfig> findByCodeAndVersion(String appCode, String appVersion);
}

在运行应用程序时出现异常

Validation failed for query for method public abstract java.util.List com.api.repository.AppConfigRepository.findByCodeAndVersion(java.lang.String,java.lang.String)!

【问题讨论】:

  • 您应该在堆栈跟踪中获得额外的输出。话虽如此,但您的 @Query 注释似乎没有任何必要。

标签: java sql spring jpa


【解决方案1】:

您必须在查询中的实体名称AppConfig之后添加别名n,它应该是这样的:

@Query("SELECT n FROM AppConfig n WHERE n.appCode = ?1 and n.appVersion = ?2")
List<AppConfig> findByCodeAndVersion(String appCode, String appVersion);

您也可以在查询字符串中使用命名参数,如下所示:

@Query("SELECT n FROM AppConfig n WHERE n.appCode = :appCode and n.appVersion = :appVersion")
List<AppConfig> findByCodeAndVersion(String appCode, String appVersion);

这样的查询可以由 Spring 数据查询方法处理,只需确保重命名该方法以使用实体的字段名称:

List<AppConfig> findByAppCodeAndAppVersion(String appCode, String appVersion);

【讨论】:

  • thanx 这行得通,但现在我没有得到任何结果,而数据库中存在数据。 Hibernate:选择 appconfig0_.id 作为 id1_0_,appconfig0_.api_url 作为 api_url2_0_,appconfig0_.app_code 作为 app_code3_0_,appconfig0_.app_name 作为 app_name4_0_,appconfig0_.app_version 作为 app_vers5_0_,appconfig0_.db_name 作为 db_name6_0_ from app_config appconfig0_ where appconfig0_.app_code=?和 appconfig0_.app_version=?
  • 您确定and 条件吗?还是传递的参数?
  • 是的,这是我要访问的 URL localhost:9090/api/v1/…。在数据库中查询我得到结果
【解决方案2】:

试试这个:

@Query("SELECT FROM AppConfig n WHERE n.appCode = :x and n.appVersion = :y")
List<AppConfig> findByCodeAndVersion(@Param("x")String appCode,@Param("y") String appVersion);

也可以直接使用方法:

List<AppConfig> findByAppCodeAndAppVersion(String appCode,String appVersion);

【讨论】:

  • 提示:尝试添加一些关于您的解决方案代码的详细信息,仅答案不是标准。
猜你喜欢
  • 1970-01-01
  • 2018-09-23
  • 2021-08-03
  • 1970-01-01
  • 2018-02-23
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多