SpringBoot JPA查询结果映射到自定义实体类


场景

举一个简单的例子:

比如有一个Position实体类

@Entity
@Table(name = "position")
public class Position implements Serializable {
    private static final long serialVersionUID = 768016840645708589L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private BigDecimal salary;
    private String city;
    
    ...//省略getset方法
    ...//省略toString方法    
}    

然后有一个PositionDetail实体类

@Entity
@Table(name = "position_detail")
public class PositionDetail implements Serializable {

    private static final long serialVersionUID = 4556211410083251360L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private Long pid;

    private String description;
    
    ...//省略getset方法
    ...//省略toString方法    
}    

需求:查询职位基本信息,职位描述,因为涉及到两张表操作,简单的查询并不能满足我们的需求,因此就需要自定义查询接口并返回符合需求的结果。

接下来再定义一个实体类,用来接收查询结果

public class PositionDO {
    private Long id;
    private String name;
    private BigDecimal salary;
    private String city;
    private String description;

    public PositionDO(Long id, String name, BigDecimal salary, String city, String description) {
        this.id = id;
        this.name = name;
        this.salary = salary;
        this.city = city;
        this.description = description;
    }

    ...//省略getset方法
    ...//省略toString方法  
}

编写Dao接口,用来实现CRUD操作

public interface PositionDao extends JpaRepository<Position, Long> {

    @Query(nativeQuery = true, value = "select t1.id, t1.name, t1.salary, t1.city, t2.description) \n" +
        "from position t1 left join position_detail t2 on t1.id = t2.pid \n" +
        "where t1.id = :id")
    PositionDO findPositionById(@Param("id") Long id);
}

思考:如果这样写会不会出现问题?接下来我们编写一个测试类测试一下。

@SpringBootTest(classes = ShardingApplication.class)
@RunWith(SpringRunner.class)
public class TestShardingDatabase {

    @Resource
    PositionDao positionDao;

    @Test
    public void testQueryById() throws Exception{
        PositionDO positionDO = positionDao.findPositionById(548083053407240192L);
        System.out.println(positionDO);
    }
}

哈哈,翻车了吧,还好先测试了一波,问题不大。

Hibernate: select t1.id, t1.name, t1.salary, t1.city, t2.description 
from position t1 left join position_detail t2 on t1.id = t2.pid 
where t1.id = ?

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.lsp.dto.PositionDO]

分析原因:那么到底是为什么造成这样的原因呢?相信小伙伴们一眼就能看出来了,是因为Jpa找不到能够从类型转换的转换器,而抛出这样的异常。

现在问题来了,既然这样不行,那么我们想要实现映射到自定义结果集该如何实现呢?

实现:JPA可以自定义SQL语句进行查询,然后查询语句可以通过原生SQL语句(原生SQL语句也就是在@Query注解里加上nativeQuery = true)进行查询。当然了,也可以通过JPQL进行查询。

我们这里就是通过JPQL进行查询,它的特征就是与原生SQL语句类似,完全面向对象,通过类名和属性访问,而不是表名和表属性。

由此PositionDao修改之后就像这样

public interface PositionDao extends JpaRepository<Position, Long> {

    @Query(value = "select new com.lsp.domain.PositionDO(t1.id, t1.name, t1.salary, t1.city, t2.description) \n" +
            "from Position t1 left join PositionDetail t2 on t1.id = t2.pid \n" +
            "where t1.id = :id")
    PositionDO findPositionById(@Param("id") Long id);
}

接下来我们再运行测试方法看一下。

Hibernate: select position0_.id as col_0_0_, position0_.name as col_1_0_, position0_.salary as col_2_0_, position0_.city as col_3_0_, positionde1_.description as col_4_0_ from position position0_ left outer join position_detail positionde1_ on (position0_.id=positionde1_.pid) where position0_.id=?
PositionDO{id=548083053407240192, name='Jerry5', salary=10000.00, city='beijing5', description='this is message 5'}

ok了,结果已经正确查询出来。

总结: 注意上面的SQL语句是面向对象的,对应的字段也都是实体类里面的属性。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM