添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
还单身的灯泡  ·  Spring Data JPA - How ...·  5 天前    · 
知识渊博的豆芽  ·  解决 Spring Data JPA ...·  5 天前    · 
温暖的剪刀  ·  hivesql ...·  8 月前    · 
没人理的豆芽  ·  yolox - 知乎·  2 年前    · 

关键字: JPA复杂查询,JPA返回自定义实体,JPA返回自定义DTO,JPA联表查询,JPA原生SQL查询,JPA踩坑。

在灵活性上JPA比不上MyBatis,比如想联表查询返回一个自定义的实体Dto,结果发现不能直接返回自定义的实体,典型错误如下:

org.springframework.core.convert.ConverterNotFoundException: 
No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [com.lmt.stock.data.XxxDto]

针对这个问题,上网搜寻一番,大概发现如下4个解决方案:

方案1.HQL查询+实体全参数的构造方法

该方法是最常见的方法

@Getter
@Setter
public class XxxDto {
    BigDecimal profitPercent;
    String accountCode;
    public XxxDto() {}
    public XxxDto(String accountCode, BigDecimal profitPercent) {
        this.accountCode = accountCode;
        this.profitPercent = profitPercent;
import com.lmt.stock.data.XxxDto;
import com.lmt.stock.data.entity.StockHisHq;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
 * @author bazhandao
 * @date 2020/3/23 11:08
 * @since JDK1.8
public interface StockHisHqRepository extends JpaRepository<StockHisHq, Long> {
    // 注意这里只能用HQL查询,不能加nativeQuery = true
    @Query(value = "select new com.lmt.stock.data.XxxDto(a.accountCode as accountCode, b.profitPercent as profitPercent) from TradeAccount a, TradeOrder b where a.accountCode = b.accountCode and a.accountCode = ?1")
    List<XxxDto> findXxxDtoByAccountCode(String accountCode);

方案2.实体定义成接口的形式(推荐)

该方式最直观!!推荐!!!

注意:YyyDtointerface接口,而不是class实体类

ps: 这里返回的是JPA生成的YyyDto的代理类,是可以直接json序列化成json字符串的

// 只需要有get方法即可,注意命名要规范
public interface YyyDto {
    String getAccountCode();
    BigDecimal getProfitPercent();
import com.lmt.stock.data.YyyDto;
import com.lmt.stock.data.entity.StockHisHq;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
 * @author bazhandao
 * @date 2020/3/23 11:08
 * @since JDK1.8
public interface StockHisHqRepository extends JpaRepository<StockHisHq, Long> {
    // 这里可以用HQL查询,也可以用原生SQL查询,YyyDto是一个接口,这里返回的是JPA生成的YyyDto的代理类
    // 查寻出的字段命名要规范,否则与接口中的get方法对应不上
    // @Query(value = "select a.account_code as accountCode, b.profit_percent as profitPercent from trade_account a, trade_order b where a.account_code = b.account_code and a.account_code = ?1", nativeQuery = true)
    @Query(value = "select a.accountCode as accountCode, b.profitPercent as profitPercent from TradeAccount a, TradeOrder b where a.accountCode = b.accountCode and a.accountCode = ?1")
    List<YyyDto> findXxxDtoByAccountCode(String accountCode);

转载自:JPA踩坑记:Spring Data Jpa 原生SQL联表查询返回自定义DTO_jpa原生sql返回自定义实体_滴水可藏海的博客-CSDN博客

如题:要实现SpringJPA中多查询返回自定义dto带分页功能,使用SpringJAP的查询时暂时没找到合适的方法,也不想强制去给实体间做关,于是有如下代码: 一、建立自己需要返回dto * 设备备品备件信息Dto数据对象信息类 @Data public class ZiYuanBeiJianDto implements Serializable { * 主键id<br> private Str
Spring Data JPA使用很方便,JPA只是一种标准,其实现通常是Hibernate。如果业务仅仅是增删改的话,那用JPA还是很方便的。但通常情况下,我们会有大量的业务查询。如果用其自带的查询,则SQL无法控制,可能会有性能问题。基于此,我们希望扩展下,使其能支持自定义查询SQL,并通过自定义POJO对象进行接收,这样我们就不需要去定义结果集所关的实体类了。 通过查看源...
在使用SpringData JPA查询的时候,一般都是返回Entity相关的结果。前段时间,在开发的时候遇到了2个查询返回自定义的实体类,这个问题楼主也是查了好多资料,才得以解决,现在分享给大家,不足之处,敬请之出。 2个实体类UserInfo、...
昨天收到一封邮件,问了我一个问题,问题如下: SELECT COUNT(user.id),user_name FROM USER where user_name like "%?%" GROUP BY user_name,id类似这种的sqlspring data jpa怎么写? 下面就这个问题一起来探讨下,怎么解决? 1、新建Entity package com.chhliu.spri