Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.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_Spring Data Jpa_Jpql - Fatal编程技术网

Java Spring JPA只返回字段,不返回实体

Java Spring JPA只返回字段,不返回实体,java,spring,spring-data-jpa,jpql,Java,Spring,Spring Data Jpa,Jpql,我有一个带注释的Spring JPA repo查询,如下所示: @Repository public interface MainRepository extends JpaRepository<MainEntity, MainEntity.ID>, JpaSpecificationExecutor<MainEntity> { @Query("select e.start, e.finish,e.forename,e.surname fro

我有一个带注释的Spring JPA repo查询,如下所示:

@Repository
public interface MainRepository 
extends 
    JpaRepository<MainEntity, MainEntity.ID>, 
    JpaSpecificationExecutor<MainEntity> {

    @Query("select e.start, e.finish,e.forename,e.surname from MainEntity e where e.volunteerId= :id "
        + "and e.areaId>0 and e.isAssignment=true order by e.start")
    List<MainEntity> findAssignments(@Param("id") int volunteerId);
}
 @Query("from MainEntity e where e.volunteerId= :id "
        + "and e.areaId>0 and e.isAssignment=true order by e.start")
@存储库
公共接口存储库
延伸
JpaRepository,
JpaSpecificationExecutor{
@查询(“从缅因州实体e中选择e.start、e.finish、e.forename、e.nam姓氏,其中e.autonovateID=:id”
+“和e.areaId>0和e.isAssignment=e.start的真实顺序”)
列出findAssignments(@Param(“id”)int-id);
}
但是,尽管返回类型不同,这并不会返回MainEntity对象的列表。它将返回与请求的字段类型相对应的对象[]列表

发生了什么事?

这样写:

@Repository
public interface MainRepository 
extends 
    JpaRepository<MainEntity, MainEntity.ID>, 
    JpaSpecificationExecutor<MainEntity> {

    @Query("select e.start, e.finish,e.forename,e.surname from MainEntity e where e.volunteerId= :id "
        + "and e.areaId>0 and e.isAssignment=true order by e.start")
    List<MainEntity> findAssignments(@Param("id") int volunteerId);
}
 @Query("from MainEntity e where e.volunteerId= :id "
        + "and e.areaId>0 and e.isAssignment=true order by e.start")
这样写:

@Repository
public interface MainRepository 
extends 
    JpaRepository<MainEntity, MainEntity.ID>, 
    JpaSpecificationExecutor<MainEntity> {

    @Query("select e.start, e.finish,e.forename,e.surname from MainEntity e where e.volunteerId= :id "
        + "and e.areaId>0 and e.isAssignment=true order by e.start")
    List<MainEntity> findAssignments(@Param("id") int volunteerId);
}
 @Query("from MainEntity e where e.volunteerId= :id "
        + "and e.areaId>0 and e.isAssignment=true order by e.start")

您不请求实体,而是请求实体的字段。而对象数组返回。 试试看:

      Select e from MainEntity e ...

您不请求实体,而是请求实体的字段。而对象数组返回。 试试看:

      Select e from MainEntity e ...

根据定义,当查询返回带有投影的列表时,JPA将返回
Object[]
列表,即来自一个实体(或多个实体)的一组字段

使用Spring Data JPA,您可以避免使用
对象[]
,并通过定义如下接口以更优雅的格式返回数据:

公共接口维护项目{
字符串getStart();
字符串getFinish();
字符串getForename();
字符串getname();
}
并更改查询方法以返回上述定义的接口:

@Query(“选择e.start、e.finish、e.forename、e.nam姓氏”+
“来自缅因州”+
“其中e.isAssignment=:id和e.areaId>0,e.isAssignment=true”+
“e.start订单”)
列出findAssignments(@Param(“id”)int-id);
本手册中描述了这种方法


除了Spring数据JPA之外,JPA本身使用公共构造函数通过
selectnew
处理它。您可以定义一个类,如下所示:

public类maintentityprojection{
私有字符串开始;
私人字符串整理;
私有字符串名;
私家姓;
公共维护项目(字符串开始、字符串结束、,
字符串名称,字符串姓氏){
this.start=start;
this.finish=完成;
this.forename=forename;
this.姓氏=姓氏;
}
//接球手和接球手
}
然后您的查询将如下所示:

选择NEW org.example.maintentityprojection(e.start、e.finish、e.forename、e.nam姓氏)
来自缅因州
其中e.isAssignment=:id和e.areaId>0,e.isAssignment=true
按e.start订购
上面的查询也可以使用Spring数据JPA(您的方法将返回
MainEntityProjection
then的列表)

检查定义JPA 2.1的文档中关于使用
SELECT NEW
和构造函数表达式的说明:

4.8.2 SELECT子句中的构造函数表达式

可以在
SELECT
列表中使用构造函数来返回Java类的实例。指定的类不需要是实体或映射到数据库。构造函数名称必须是完全限定的

如果在
SELECT NEW
子句中将实体类名称指定为构造函数名称,则生成的实体实例将处于新建或分离状态,具体取决于是否为构造的对象检索主键

如果作为构造函数参数的单值路径表达式或标识变量引用实体,则该单值路径表达式或标识变量引用的结果实体实例将处于托管状态

比如说,

选择NEW com.acme.example.CustomerDetails(c.id、c.status、o.count)
从客户c加入c订单o
其中o.计数>100

根据定义,当查询返回带有投影的列表时,JPA将返回
Object[]
列表,即来自一个实体(或多个实体)的一组字段

使用Spring Data JPA,您可以避免使用
对象[]
,并通过定义如下接口以更优雅的格式返回数据:

公共接口维护项目{
字符串getStart();
字符串getFinish();
字符串getForename();
字符串getname();
}
并更改查询方法以返回上述定义的接口:

@Query(“选择e.start、e.finish、e.forename、e.nam姓氏”+
“来自缅因州”+
“其中e.isAssignment=:id和e.areaId>0,e.isAssignment=true”+
“e.start订单”)
列出findAssignments(@Param(“id”)int-id);
本手册中描述了这种方法


除了Spring数据JPA之外,JPA本身使用公共构造函数通过
selectnew
处理它。您可以定义一个类,如下所示:

public类maintentityprojection{
私有字符串开始;
私人字符串整理;
私有字符串名;
私家姓;
公共维护项目(字符串开始、字符串结束、,
字符串名称,字符串姓氏){
this.start=start;
this.finish=完成;
this.forename=forename;
this.姓氏=姓氏;
}
//接球手和接球手
}
然后您的查询将如下所示:

选择NEW org.example.maintentityprojection(e.start、e.finish、e.forename、e.nam姓氏)
来自缅因州
其中e.isAssignment=:id和e.areaId>0,e.isAssignment=true
按e.start订购
上面的查询也可以使用Spring数据JPA(您的方法将返回
MainEntityProjection
then的列表)

检查定义JPA 2.1的文档中关于使用
SELECT NEW
和构造函数表达式的说明:

4.8.2选择中的构造函数表达式