Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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
Java Spring数据JPA-存储库返回不同的类型_Java_Spring_Hibernate_Spring Data Jpa - Fatal编程技术网

Java Spring数据JPA-存储库返回不同的类型

Java Spring数据JPA-存储库返回不同的类型,java,spring,hibernate,spring-data-jpa,Java,Spring,Hibernate,Spring Data Jpa,我有一个像这样的crudepository: public interface TestTableRepository extends CrudRepository<TestTable, Long> { @Query( value = "select count(case when field1 = true then 1 end) as field1Count, count(case when field2 = true then 1 e

我有一个像这样的
crudepository

public interface TestTableRepository extends CrudRepository<TestTable, Long> {

    @Query(
            value = "select count(case when field1 = true then 1 end) as field1Count, count(case when field2 = true then 1 end) as field2Count from test_table",
            nativeQuery = true
    )
    List<Integer> selectRowCount();
    
}
公共接口TestTableRepository扩展了Crudepository{
@质疑(
value=“从测试表中选择count(当field1=true然后1结束时的情况)作为field1Count,count(当field2=true然后1结束时的情况)作为field2Count”,
nativeQuery=true
)
列出selectRowCount();
}
这是我的应用程序中类似查询的简化版本。使用的最佳退货类型是什么

正如当前编写的那样,实际返回的类型是
List
,而不是
List
。这怎么可能?我猜这与Spring/Hibernate在运行时构建的查询实现有关。

**这种情况下最好的返回类型是“Map”。
**The best return type in this case is `Map<String, Object>`.<br/>
To retrieve data from Map you can used alias name. In this case, key field1Count and field2Count will give you count value.**    
        
For Example
@RestController class :
            @GetMapping("/getCount")
                public String getCounnt() {
                    Map<String, Object> mp =  userRepo.getCount();
                    int field1CountValue = Integer.parseInt(mp.get("field1Count").toString());
                    int field2CountValue = Integer.parseInt(mp.get("field2Count").toString());  
                    System.out.println("field1CountValue :"+field1CountValue+" field2CountValue :"+field2CountValue);
                    return "field1CountValue :"+field1CountValue+" field2CountValue :"+field2CountValue;
                }
    ___________________________________
@Repository Interface
            import java.util.Map;
            import org.springframework.data.jpa.repository.Query;
            import org.springframework.data.repository.CrudRepository;
            
            public interface UserRepo extends CrudRepository<User, Long>{
            
                @Query(
                        value = "select count(case when id > 0 then 1 end) as field1Count, "
                                + "count(case when id > 15 then 1 end) as field2Count from user",
                        nativeQuery = true
                )
                Map<String, Object> getCount(); 
            }
Console :
[Hibernate: select count(case when id > 0 then 1 end) as field1Count, count(case when id > 15 then 1 end) as field2Count from user
field1CountValue :9 field2CountValue :0]
要从地图检索数据,可以使用别名。在这种情况下,键field1Count和field2Count将为您提供计数值。** 例如 @RestController类: @GetMapping(“/getCount”) 公共字符串getconnt(){ Map mp=userRepo.getCount(); int field1CountValue=Integer.parseInt(mp.get(“field1Count”).toString(); int field2CountValue=Integer.parseInt(mp.get(“field2Count”).toString(); System.out.println(“field1CountValue:+field1CountValue+”field2CountValue:+field2CountValue”); 返回“field1CountValue:”+field1CountValue+“field2CountValue:”+field2CountValue; } ___________________________________ @存储库接口 导入java.util.Map; 导入org.springframework.data.jpa.repository.Query; 导入org.springframework.data.repository.crudepository; 公共接口UserRepo扩展了Crudepository{ @质疑( value=“选择计数(id>0时为1结束)作为field1Count,” +“将(当id>15时,则为1结束)计数为用户的field2Count”, nativeQuery=true ) 映射getCount(); } 慰问: [Hibernate:选择count(当id>0时,案例1结束)作为field1Count,count(当id>15时,案例1结束)作为field2Count from user field1CountValue:9 field2CountValue:0]
请解释为什么使用此方法,而不仅仅是使用现有的
count()
方法。您的计数条件不仅仅是
*
?是的,我的应用程序中的实际查询类似于
选择count(当field1=true然后1 end时为case)作为field1Count,count(当field2=true然后1 end时为case)作为field2Count
使用本机查询选择多个col的默认行为是
List
,您可以将其转换为列表注意,
nativeQuery
应该是最后的选择;你能把你的查询换成JPQL吗?
nativeQuery
当然不是最后的选择。它使许多事情变得更容易/更快/可能,并且许多项目由于使用本机功能而几乎毫无进展。我尝试了它,得到了:
,原因是:org.springframework.core.convert.ConverterNotFoundException:没有找到能够从[java.math.biginger]类型转换为[java.util.Map]类型的转换器
请检查更新的解决方案尝试新的解决方案。同样的错误。