简体   繁体   English

无法使用 JPA 查询检索结果

[英]Unable to retrieve results using JPA query

I am trying to build a Spring Boot REST API that fetches data from a MySQL db.我正在尝试构建一个 Spring 引导 REST API 从 Z62A004B957146BB97AZ34DCAFA 获取数据This is the JPA query method I have defined in my Repository code of my Spring Boot REST API application:这是我在 Spring 引导 REST ZDB974A7387143CADED1Z 应用程序的存储库代码中定义的 JPA 查询方法

public interface WeekRepository extends CrudRepository<Week, UUID> {
           Iterable<Week> findByOriginId(UUID originId);
}

This is the details of the table that I want to query:这是我要查询的表的详细信息:

CREATE TABLE `week` 
  ( 
     `id`        DOUBLE NOT NULL auto_increment, 
     `due_date`  TIMESTAMP NOT NULL, 
     `origin_id` BINARY(36) DEFAULT NULL, 
     `status`    VARCHAR(36) DEFAULT NULL, 
     PRIMARY KEY (`id`) 
  )  

I have turned on logging in my Spring Boot application as a result I can see the query generated by Hibernate looks like this:我打开了登录我的 Spring 引导应用程序,结果我可以看到 Hibernate 生成的查询如下所示:

SELECT week0_.id        AS id1_3_, 
       week0_.due_date  AS due_date2_3_, 
       week0_.origin_id AS origin_i3_3_, 
       week0_.status    AS status4_3_ 
FROM   week week0_ 
WHERE  week0_.origin_id = ?; 

From the logs I can see that I am passing my parameter, origin_id in the JSON successfully through the controller to the repository code.从日志中我可以看到我正在通过 controller 成功地将 JSON 中的参数 origin_id 传递给存储库代码。 The log print out in the console reveals that the parameter is being bound to the?控制台中的日志打印显示该参数被绑定到? parameter as shown below:参数如下图:

2021-01-15 00:44:05.916 TRACE 44612 --- [nio-8082-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [49ee49da-8e6e-45f0-a0ea-e63205077870]

However there is no result being returned by this query.但是,此查询没有返回任何结果。

Can someone help me?有人能帮我吗? Please do let me know if you need more information from my side.如果您需要我方面的更多信息,请告诉我。 Thank you.谢谢你。

Please add below annotation to originId property to make hibernate to add necessary conversions.请在 originId 属性中添加以下注释,以使 hibernate 添加必要的转换。 There seems to be some issue with UUID props for mysql mysql 的 UUID 道具似乎存在一些问题

@Column(name = "origin_id")
@Type(type="uuid-char")
private UUID originId;

You can also use @Query annotation in your weekRepository interface to get the desired result like this:您还可以在 weekRepository 界面中使用 @Query 注释来获得所需的结果,如下所示:

@Query(value="select * from week0 where week0_.origin_id = ?1",nativeQuery=true )
Iterable<Week> findByOriginId(UUID originId);

Here I assumed that week0 is the database table name, so check it and change accordingly and let me know if this works.在这里我假设 week0 是数据库表名,所以检查它并相应地更改,让我知道这是否有效。

Try out this:试试这个:

@Query(value="select * from week where origin_id = :originId", nativeQuery=true)
Iterable<Week> findByOriginId(UUID originId);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM