Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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

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
Spring @查询返回对象而不是实体_Spring_Jpa_Spring Data Jpa_Spring Data - Fatal编程技术网

Spring @查询返回对象而不是实体

Spring @查询返回对象而不是实体,spring,jpa,spring-data-jpa,spring-data,Spring,Jpa,Spring Data Jpa,Spring Data,当我将@Query注释与选择字段一起使用时,我不会返回实体对象。如何取回实体对象 public interface CreditCenterRepository extends JpaRepository<CreditCenter, Long> { @Query("SELECT CC.id, CC.loanId, CC.clientId FROM CreditCenter CC") List<CreditCenter> findAllIds(); } 公

当我将@Query注释与选择字段一起使用时,我不会返回实体对象。如何取回实体对象

public interface CreditCenterRepository extends JpaRepository<CreditCenter, Long> {
    @Query("SELECT CC.id, CC.loanId, CC.clientId FROM CreditCenter CC")
    List<CreditCenter> findAllIds();
}
公共界面CreditCenterRepository扩展了JpaRepository{
@查询(“从CreditCenter CC中选择CC.id、CC.loanId、CC.clientId”)
列出findAllIds();
}
当我从我的控制器调用这个方法时,它不会抛出任何错误,但是,当我尝试迭代时,它抛出classcastexception

List<CreditCenter> objects = findAllIds();
for (CreditCenter cc : objects) { //This line throws ClassCastException
    //some logic
}
List objects=findAllIds();
对于(CreditCenter cc:objects){//此行抛出ClassCastException
//一些逻辑
}

好的,您似乎正在使用实体CreditCenter上的投影

@Query("SELECT CC.id, CC.loanId, CC.clientId FROM CreditCenter CC")
我的第一个想法是: 为什么你不使用这样的东西

 @Query("SELECT CC FROM CreditCenter CC")
这将返回实体列表,但是您可能不想返回所有字段,因此我的第二个建议是使用此查询

@Query("SELECT new package.to.CreditCenter(CC.id, CC.loanId, CC.clientId) FROM CreditCenter CC")
并在Creditcenter中添加支持参数顺序和类型的构造函数。这将在使用JPQL和jpa回购协议时起作用

public class CreditCenter {

 //Member vars
 public CreditCenter (int id, int loadid, int clientid){...}
}

如果您希望有选择地返回实体的一部分,建议在JPA中使用专用DTO类型,也称为投影类。对于这个特定的查询,您可以进行如下操作:

class CreditCenterExcerpt {

  private int id, loanId, clientId;

  public CreditCenterExcerpt(int id, int loadid, int clientid) { … }
}
然后以一种类似的方式使用它

接口CrediCenter存储库实现存储库{
@查询(“从CreditCenter CC中选择新的….CreditCenter摘录(CC.id、CC.loanId、CC.clientId))
列出您的特殊查询方法();
}
我之所以将此作为一个单独的答案来写,是因为使用实体类型本身有很多缺点(因此首先建议使用单独的类型):

无论
EntityManager
是否仍处于打开状态,从投影查询执行返回的实例都会根据定义进行分离。通过为这些调用使用单独的类型,您不会意外地产生返回的对象是完全填充和托管的实体实例的印象


不知道投影的开发人员可能只使用返回的实例,尝试访问未填充的属性,甚至尝试依次保存实例,从而清除所有未加载的属性。

根据其他注释,我将其更改为-选择新的CreditCenter(CC.id,CC.loanId,CC.clientId)从信用中心抄送。这将返回实体。这是最好的方法吗?
interface CrediCenterRepository implements Repository<CreditCenter, Integer> {

  @Query("select new ….CreditCenterExcerpt(CC.id, CC.loanId, CC.clientId) from CreditCenter CC")
  List<CreditCenterExcerpt> yourSpecialQueryMethod();
}