如何使用JPA查询方法从表中选择两列的所有数据 [英] How to select all the data of two columns from a table using JPA query method
本文介绍了如何使用JPA查询方法从表中选择两列的所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
SELECT ID,NAME FROM TABLE_NAME
此SQL查询按预期工作,但我想把它写成JPA查询方法,我尝试了很多方法,但没有得到它的工作,请帮助我。
以下我试过但不起作用:
$ p $ findAllByIdName(){}
findAllByIdAndName(){}
findByIdName() ){}
findByIdAndName(){}
解决方案
首先创建一个结果类:
package com.example;
$ p
public class ResultClass {
private Long id;
私人字符串名称;
public ResultCalss(Long id,String name){
// set
}
}
$ b,然后使用自定义的@Query:
$ b $ pre $ @ $ c $ @从MyEntity e中选择新的com.example.ResultClass(e.id,e.name))
public List< ResultClass> findIdsAndNames();
I want to add in my Repository
interface a method for a following SQL query:
SELECT ID, NAME FROM TABLE_NAME
This SQL query works as expected, but I want to write it as a JPA query method, I've tried in many ways but didn't get it working, Please help me.
Following which I've tried but didn't work:
findAllByIdName(){}
findAllByIdAndName(){}
findByIdName(){}
findByIdAndName(){}
解决方案
Create a result class first:
package com.example;
public class ResultClass{
private Long id;
private String name;
public ResultCalss(Long id, String name){
// set
}
}
and then use a custom @Query:
@Query("select new com.example.ResultClass(e.id, e.name) from MyEntity e")
public List<ResultClass> findIdsAndNames();
这篇关于如何使用JPA查询方法从表中选择两列的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文