Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从JPA存储库返回特定属性?_Jpa_Repository - Fatal编程技术网

如何从JPA存储库返回特定属性?

如何从JPA存储库返回特定属性?,jpa,repository,Jpa,Repository,在将实体检索到资源中时,我得到一个ClassCastException。仅当JPA repository语句显式指定要返回的属性时,才会发生此错误 例如,这工作正常,没有触发异常: @Query("SELECT r FROM Rollout r, RolloutAdmin ra WHERE r.id = ra.rollout.id AND ra.admin = :admin") 它呈现为: select rollout0_.id as id1_8_, rollout0_.version as

在将实体检索到资源中时,我得到一个ClassCastException。仅当JPA repository语句显式指定要返回的属性时,才会发生此错误

例如,这工作正常,没有触发异常:

@Query("SELECT r FROM Rollout r, RolloutAdmin ra WHERE r.id = ra.rollout.id AND ra.admin = :admin")
它呈现为:

select rollout0_.id as id1_8_, rollout0_.version as version2_8_, rollout0_.bts_nb_all as bts_nb_a3_8_, rollout0_.country_id as country_9_8_, rollout0_.creation_datetime as creation4_8_, rollout0_.description as descript5_8_, rollout0_.end_datetime as end_date6_8_, rollout0_.name as name7_8_, rollout0_.operator_id as operato10_8_, rollout0_.start_datetime as start_da8_8_ from rollout rollout0_ cross join rollout_admin rolloutadm1_ where rollout0_.id=rolloutadm1_.rollout_id and rolloutadm1_.admin_id=1 order by rollout0_.name asc limit 10;
select distinct rollout0_.id as col_0_0_, rollout0_.country_id as col_1_0_, rollout0_.operator_id as col_2_0_, rollout0_.name as col_3_0_, rollout0_.description as col_4_0_, rollout0_.creation_datetime as col_5_0_, rollout0_.start_datetime as col_6_0_, rollout0_.end_datetime as col_7_0_, country3_.id as id1_4_0_, operator4_.id as id1_6_1_, country3_.version as version2_4_0_, country3_.code as code3_4_0_, country3_.list_order as list_ord4_4_0_, country3_.name as name5_4_0_, operator4_.version as version2_6_1_, operator4_.country_id as country_9_6_1_, operator4_.description as descript3_6_1_, operator4_.image as image4_6_1_, operator4_.messenger as messenge5_6_1_, operator4_.name as name6_6_1_, operator4_.operator_id as operator7_6_1_, operator4_.url as url8_6_1_ from rollout rollout0_ inner join country country3_ on rollout0_.country_id=country3_.id inner join operator operator4_ on rollout0_.operator_id=operator4_.id cross join rollout_admin rolloutadm1_ cross join bts bts2_ where bts2_.rollout_id=rollout0_.id and rollout0_.id=rolloutadm1_.rollout_id and rolloutadm1_.admin_id=1 group by rollout0_.id order by rollout0_.name asc limit 10;
但这引发了例外:

@Query("SELECT DISTINCT r.id, r.country, r.operator, r.name, r.description, r.creationDatetime, r.startDatetime, r.endDatetime FROM Rollout r, RolloutAdmin ra, BTS b WHERE b.rollout.id = r.id AND r.id = ra.rollout.id AND ra.admin = :admin GROUP BY r.id")
它呈现为:

select rollout0_.id as id1_8_, rollout0_.version as version2_8_, rollout0_.bts_nb_all as bts_nb_a3_8_, rollout0_.country_id as country_9_8_, rollout0_.creation_datetime as creation4_8_, rollout0_.description as descript5_8_, rollout0_.end_datetime as end_date6_8_, rollout0_.name as name7_8_, rollout0_.operator_id as operato10_8_, rollout0_.start_datetime as start_da8_8_ from rollout rollout0_ cross join rollout_admin rolloutadm1_ where rollout0_.id=rolloutadm1_.rollout_id and rolloutadm1_.admin_id=1 order by rollout0_.name asc limit 10;
select distinct rollout0_.id as col_0_0_, rollout0_.country_id as col_1_0_, rollout0_.operator_id as col_2_0_, rollout0_.name as col_3_0_, rollout0_.description as col_4_0_, rollout0_.creation_datetime as col_5_0_, rollout0_.start_datetime as col_6_0_, rollout0_.end_datetime as col_7_0_, country3_.id as id1_4_0_, operator4_.id as id1_6_1_, country3_.version as version2_4_0_, country3_.code as code3_4_0_, country3_.list_order as list_ord4_4_0_, country3_.name as name5_4_0_, operator4_.version as version2_6_1_, operator4_.country_id as country_9_6_1_, operator4_.description as descript3_6_1_, operator4_.image as image4_6_1_, operator4_.messenger as messenge5_6_1_, operator4_.name as name6_6_1_, operator4_.operator_id as operator7_6_1_, operator4_.url as url8_6_1_ from rollout rollout0_ inner join country country3_ on rollout0_.country_id=country3_.id inner join operator operator4_ on rollout0_.operator_id=operator4_.id cross join rollout_admin rolloutadm1_ cross join bts bts2_ where bts2_.rollout_id=rollout0_.id and rollout0_.id=rolloutadm1_.rollout_id and rolloutadm1_.admin_id=1 group by rollout0_.id order by rollout0_.name asc limit 10;
为什么会有这样的差别?有没有办法指定要返回的属性而不获取整个依赖父属性

编辑:多亏了Nizet,我现在明白了,我需要返回一个与我的存储库方法类型相同的对象。

非常简单:

SELECT r FROM Rollout r
意思是:我希望此查询返回r,类型为Rollout。查询返回一个列表

鉴于

SELECT DISTINCT r.id, r.country, r.operator, r.name, r.description, r.creationDatetime, r.startDatetime, r.endDatetime FROM Rollout r
意思:我希望此查询返回许多不同的内容:ID、国家/地区、运算符等。由于查询返回许多不同的内容、不同的类型,并且可能来自多个不同的实体(在本示例中并非如此),但您也可以从ra返回属性,因此查询返回一个列表,其中,对象数组的第一个元素是ID,第二个是country,第三个是operator,等等

并不是说即使是第一个查询也比它应该的复杂。这两个实体之间的关联已经说明了这两个实体是如何连接的。所以你只需要

SELECT r FROM RolloutAdmin ra join ra.rollout r WHERE ra.admin = :admin

这是有道理的。但是,既然我的查询返回:public页,难道它不能得到它是一个卷展栏吗?因此,在资源汇编环境中,对象不能被强制转换到卷展栏,必须使用SELECT r FROM表单,并且不能有特定的列?实体管理器不知道您的方法返回什么。它只知道作为参数传递的查询。是的,如果希望查询返回卷展栏实例,则查询必须选择别名或卷展栏。不是属性列表。好吧,我想我会尝试使用QueryDSL,因为我的原始查询具有sum和count函数的结果属性。大多数情况下,单个查询优于2。但您也可以使用select r、count…、sum…、。。。。您将获得对象数组,其中包含索引为0的卷展栏实例。