简体   繁体   English

如何从具有外键类型的JPA存储库中返回选择查询

[英]How to return a select query from jpa repository with foreign key type

I am trying to do this: 我正在尝试这样做:

@Query(value = "SELECT DISTINCT c.* FROM comarca c INNER JOIN debito_negativacao d ON d.comarca_id = c.id WHERE d.status = :status", nativeQuery = true)
List<Comarca> findDistinctComarcaByStatus(@Param("status") String status);

But I get this error: 但是我得到这个错误:

  org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [com.hc.projects.model.Comarca] for value '{9, 0, 7323}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.math.BigInteger] to type [com.hc.projects.model.Comarca]

You have to return all columns that are necessary to construct a Comarca. 您必须返回构造Comarca所需的所有列。 So you will have to join the table. 因此,您将必须加入表格。

As didn't provide the tables I can only guess: 由于没有提供表格,我只能猜测:

@Query(value = "SELECT DISTINCT * FROM comarca c " +
                "JOIN debito_negativacao d ON d.comarca_id = c.id "+
                "WHERE d.debito_negativacao.status= :status", nativeQuery = true)
List<Comarca> findDistinctComarcaIdByStatus(@Param("status") String status);

Your request tells that you want a list of BigInteger : SELECT DISTINCT comarca_id... because comarca_id is a biginteger I guess. 您的请求告诉您需要BigInteger的列表:SELECT DISTINCT comarca_id ...因为我想comarca_id是biginteger。 If you want a Comarca list, you have to request on all your table. 如果您想要一个Comarca列表,则必须在所有表格上提出要求。

If you want to use a distinct query that will return other columns than the distinct one you need some kind of strategy how to "merge" the objects. 如果要使用一个独特的查询,该查询将返回不同于该独特查询的其他列,则需要某种策略来“合并”对象。 Imagine rows like this: 想象这样的行:

------------------------------
| comarca_id| key | status    |
| 1         | A   | your_state|
| 1         | B   | your_state|
| 2         | C   | your_state|
------------------------------

What would you get in this case? 在这种情况下,您会得到什么?

SELECT DISTINCT comarca_id FROM comarca; will return 1,2 将返回1,2

However, how can you merge two (or more) entries which have the same comarca_id and status ? 但是,如何合并两个(或多个)具有相同comarca_idstatus条目?

This leaves you with three cases: 剩下三种情况:

  1. you assume comarca_id + status is unique -> you don't need the DISTINCT query 您假设comarca_id + status是唯一的->您不需要DISTINCT查询
  2. There might be more than one row with same comarca_id and status -> you can't make the query distinct 可能有不止一排具有相同的comarca_idstatus ->您无法区分查询
  3. you only want the distinct comarca_id values -> make your method return List<BigInteger> 您只需要不同的comarca_id值->使您的方法返回List<BigInteger>

If, in a second time, you want to isolate a list of comarca_id, try to stream your request result. 如果您第二次想要隔离comarca_id列表,请尝试流式传输您的请求结果。

    List<Comarca> comarca = debitoNegativacao.stream().map(dn -> dn.getComarca()).distinct().collect(Collectors.toList());

++ ++

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

相关问题 JPA存储库返回类型 - JPA repository return type 如何使用外键在 Spring Data/JPA 中执行查询? - How to perform a query in Spring Data /JPA with a foreign Key? 如何使 Jpql 查询在 Spring Jpa 存储库中使用可选返回? - How to make a Jpql query work in Spring Jpa Repository with Optional return? 数据库中的JPA外键,项目中的返回对象 - JPA Foreign key in database, return object in project 使用 JPA 标准 API 为外键创建查询 - Create query with JPA Criteria API for foreign key 生成外键字段的JPA查询 - Generate JPA query for foreign key field 如何使用 Spring JPA 存储库正确保存带有外键的 object? - How do i use Spring JPA repository to correctly save an object with a foreign key? Java:如何分配或读取从 postgresql 方法返回的 JSON 数据查询到 Z9CE3D1Z48930F196A0764 存储库。 [查询:SELECT row_to_json(…)] - Java: How to assign or read JSON data returned from a postgresql Query into JPA Repository method. [query: SELECT row_to_json(…)] 使用带有可为空外键的 QueryDSL 在 Spring Data JPA Repository 中进行过滤 - Filtering in Spring Data JPA Repository using QueryDSL with a nullable foreign key 如何使用JPA / JPQL查询和外键从数据库读取数据 - How to read data from database with JPA/JPQL Query and Foreign Keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM