前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringDataJPA笔记(8)-EntityManager

SpringDataJPA笔记(8)-EntityManager

作者头像
yingzi_code
发布2019-08-31 12:22:40
1.9K0
发布2019-08-31 12:22:40
举报

EntityManager,以及union查询,left join查询

EntityManager是JPA中用于增删改查的接口,而通常我们使用的是Hibernate-EntityManager

Hibernate-EntityManager实现了标准的JPA,可以把它看成hibernate-core和JPA之间的适配器,它并不直接提供ORM的功能,而是对hibernate-core进行封装,使得Hibernate符合JPA的规范

使用案例
代码语言:javascript
复制
    @ApiOperation(value = "查询", httpMethod = "GET")
    @GetMapping("/search")
    public List<CatView> search() {
        Query query = entityManager.createNativeQuery("select id as id, name as name from cat_tb");

        List<CatView> list = query.getResultList();
        log.info("list: {}", JSON.toJSONString(list));
        return list;
    }
    @ApiOperation(value = "查询union", httpMethod = "GET")
    @GetMapping("/search/union")
    public List<CatView> searchUnion() {
        Query query = entityManager.createNativeQuery("select c.id as id, c.name as name from cat_tb c  union all select d.id as id, d.name as name from dog_tb d");

        List<CatView> list = query.getResultList();
        log.info("list: {}", JSON.toJSONString(list));
        return list;
    }
在使用JPA的时候,是不支持union查询的,只能使用原生sql查询,且在使用Query注解的时候无法使用普通类去接收查询的数据

使用Query注解方法

代码语言:javascript
复制
    @Query(nativeQuery = true, value = "select c.id as id, c.name as name from cat_tb c  union all select d.id as id, d.name as name from dog_tb d",
            countQuery = "select count(*) from (select c.id as id from cat_tb c  union all select d.id as id from dog_tb d) a")
    <T> Page<T> findUnion(Pageable pageable, Class<T> type);

使用普通类和接口类去接收查询数据

代码语言:javascript
复制
    @ApiOperation(value = "union query查询", httpMethod = "GET")
    @GetMapping("/left/union")
    public Page<CatView> findUnion(@RequestParam int pageSize, @RequestParam int pageNum) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        return catRepository.findUnion(pageable, CatView.class);
    }

    @ApiOperation(value = "union query查询2", httpMethod = "GET")
    @GetMapping("/left/union2")
    public Page<CatView2> findUnion2(@RequestParam int pageSize, @RequestParam int pageNum) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        return catRepository.findUnion(pageable, CatView2.class);
    }

第一个方法会报错,第二个方法会得到正确的数据

第一个类是普通类

代码语言:javascript
复制
@Data
@AllArgsConstructor
public class CatView {

    private Long id;
    private String name;
}

第二个是接口类

代码语言:javascript
复制
public interface CatView2 {

    Long getId();
    String getName();
}
补充使用left join的案例,均得到正确的返回
代码语言:javascript
复制
    @Query("select new com.mt.demo.jpa.entity.view.CatView(c.id , d.name) from CatEntity c left join DogEntity d on c.id = d.id")
    <T> Page<T> findQuery(Pageable pageable, Class<T> type);

    @Query("select c.id as id, d.name as name from CatEntity c left join DogEntity d on c.id = d.id")
    <T> Page<T> findQuery2(Pageable pageable, Class<T> type);
代码语言:javascript
复制
    @ApiOperation(value = "left join query查询", httpMethod = "GET")
    @GetMapping("/left/join")
    public Page<CatView> leftJoin(@RequestParam int pageSize, @RequestParam int pageNum) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        return catRepository.findQuery(pageable, CatView.class);
    }

    @ApiOperation(value = "left join query查询2", httpMethod = "GET")
    @GetMapping("/left/join2")
    public Page<CatView2> leftJoin2(@RequestParam int pageSize, @RequestParam int pageNum) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        return catRepository.findQuery2(pageable, CatView2.class);
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年05月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用案例
  • 在使用JPA的时候,是不支持union查询的,只能使用原生sql查询,且在使用Query注解的时候无法使用普通类去接收查询的数据
  • 补充使用left join的案例,均得到正确的返回
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档