Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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实体到DTO的转换_Spring_Spring Boot_Jpa_Spring Data Jpa - Fatal编程技术网

Spring实体到DTO的转换

Spring实体到DTO的转换,spring,spring-boot,jpa,spring-data-jpa,Spring,Spring Boot,Jpa,Spring Data Jpa,假设我有一个a类,比如: public class A{ Long aa; } 并在其上扩展了Jpa存储库。现在,ARepo扩展了Jp… 现在我有另一个DTO B,如: public class B{ Long bb; Long cc; } 我偶尔会使用这个DTO,这就是为什么我不直接将它保存在DB中 现在我要做的是: List<A>as=aRepo.getAll(); List<B>bs=new ArrayList<>(); for(

假设我有一个a类,比如:

public class A{
    Long aa;
}
并在其上扩展了Jpa存储库。现在,
ARepo扩展了Jp…

现在我有另一个DTO B,如:

public class B{
   Long bb;
   Long cc;
}
我偶尔会使用这个DTO,这就是为什么我不直接将它保存在DB中

现在我要做的是:

List<A>as=aRepo.getAll();
List<B>bs=new ArrayList<>();
for(A a:as)
    bs.add(new B(a));
Listas=aRepo.getAll();
Listbs=newarraylist();
对于(A:as)
增加(新的B(a));

我不喜欢这样,而且速度似乎很慢,如何解决这个问题?

您可能想尝试使用JPQL
NEW
操作符:

public interface ARepo extends JpaRepository<A, Long> {
    @Query("SELECT NEW org.example.whatever.B(a) FROM A as a;")
    List<B> findAllAsBs();
    // all the other possible stuf...
}
公共接口ARepo扩展了JpaRepository{
@查询(“从a中选择NEW org.example.whatever.B(a)作为a;”)
List findAllAsBs();
//所有其他可能的问题。。。
}
您可能需要调整某些方面,但原则是正确的