[英]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_id
和status
条目?
This leaves you with three cases: 剩下三种情况:
comarca_id
+ status
is unique -> you don't need the DISTINCT
query comarca_id
+ status
是唯一的->您不需要DISTINCT
查询 comarca_id
and status
-> you can't make the query distinct comarca_id
和status
->您无法区分查询 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.