Suppose I have a class A like:
public class A{
Long aa;
}
And a Jpa Repository been extended on it. Now ARepo extends Jp...<A,Long>
Now I have have another DTO B like:
public class B{
Long bb;
Long cc;
}
I use this DTO once in a while, that's why I don't directly save it in DB.
Now what I'm doing is:
List<A>as=aRepo.getAll();
List<B>bs=new ArrayList<>();
for(A a:as)
bs.add(new B(a));
I don't like this and it seems real slow, how to resolve this ?
You might want to try JPQL NEW
operator:
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...
}
You might need to adjust some aspects, but the principle.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments