JPA自定义对象接收查询结果集操作

最近使用JPA的时候,碰到需要自定义查询结果集的场景,网上搜了一下,都是需要自定义方法写一大串代码实现的,太繁琐了,有那时间还不如用mybaits。

用JPA就是要尽量通过声明接口解决持久层问题,要不然鬼用。逼得没办法去了官网看看文档,再没有就放弃了,没时间看源码。最终找到我想要的结果了。

例如,传统的JPA接口实现如下所示:

class Person {

@Id UUID id;

String firstname, lastname;

Address address;

static class Address {

String zipCode, city, street;

}

}

interface PersonRepository extends Repository<Person, UUID> {

Collection<Person> findByLastname(String lastname);

}

自定义对象接收查询结果集方法如下:

(1)增加接收数据接口

interface NamesOnly {

String getFirstname();

String getLastname();

}

(2)增加持久层接口

interface PersonRepository extends Repository<Person, UUID> {

Collection<NamesOnly> findByLastname(String lastname);

}

如果要对查询结果进行序列号的话就会有点问题:

{

"errorCode": "00",

"errorMessage": "操作成功",

"returnObject": [

{

"createtime": 1526358195000,

"id": 49,

"lastupdatetime": 1526358195000,

"status": "2",

"target": {

"createtime": 1526358195000,

"lastupdatetime": 1526358195000,

"check_Wicket": "1",

"facility_name": "血压测量",

"facility_Num": "C3",

"id": 49,

"status": "2",

"check_name": "小汤154",

"check_Num": "BY185201805140001"

},

"targetClass": "org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap"

}

]

}

会出现targetClass这个字段,不能直接把结果拿来用,很恶心,又不想写代码中转下。

经过后来的摸索,其实如果只是为了返回JSON,也可以直接在Repository层直接用List<Map<String,Object>>来返回,

Map<String,Object>对应单条查询结果,完美解决序列化问题。

完毕。就这么简单。

补充: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语句是面向对象的,对应的字段也都是实体类里面的属性。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

以上是 JPA自定义对象接收查询结果集操作 的全部内容, 来源链接: utcz.com/z/317578.html

回到顶部