如何使用外键类型从 jpa 存储库返回选择查询

我正在尝试这样做:


@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);

但我得到这个错误:


  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]


www说
浏览 191回答 3
3回答

Qyouu

如果第二次要隔离comarca_id 列表,请尝试流式传输您的请求结果。&nbsp;&nbsp;&nbsp;&nbsp;List<Comarca>&nbsp;comarca&nbsp;=&nbsp;debitoNegativacao.stream().map(dn&nbsp;->&nbsp;dn.getComarca()).distinct().collect(Collectors.toList());++

Cats萌萌

您必须返回构造 Comarca 所需的所有列。所以你必须加入表。由于没有提供表格,我只能猜测:@Query(value = "SELECT DISTINCT * FROM comarca c " +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "JOIN debito_negativacao d ON d.comarca_id = c.id "+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "WHERE d.debito_negativacao.status= :status", nativeQuery = true)List<Comarca> findDistinctComarcaIdByStatus(@Param("status") String status);

浮云间

您的请求告诉您想要一个 BigInteger 列表: SELECT DISTINCT comarca_id... 因为我猜comarca_id 是一个 biginteger。如果你想要一份 Comarca 名单,你必须在你的所有桌子上提出要求。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java