Spring entity to DTO conversion

Maifee Ul Asad :

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 ?

pirho :

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.

edited at
0

Comments

0 comments
Login to comment

Related

Spring Rest API validation should be in DTO or in entity?

Conversion of DTO to entity and vice-versa

Using Entity as DTO in Hibernate

Where to put Entity To DTO transformation in a Spring boot project?

Spring HATEOAS and Entity DTO - problem with ModelAssembler

Mixing entity with payload (dto) in spring boot - best practice

Naming DTO and Entity classes

DTO and Entity in one object?

Javers compare entity to DTO

How to convert from entity to dto using model mapper , with conversion from string to UUID

java spring shortcut to create dto of an entity with customization ability

Cast entity to dto

I have created a spring boot with DTO, Entity and also not able to store it

How can convert DTO to Entity in Spring boot?

Map dto to complex entity

Dto to Entity conversion using MapStruct gives error

AutoMapper Generic conversion of DTO to Domain Entity in Service Layer

Validation a DTO with two entity

Dto to entity and dto from entity

Spring webflux convert Entity class to Mono DTO object

If i am converting a entity to a DTO where this conversion code should exist?

If Service returns DTO not entity, how to handle DTO in other service in Spring Boot?

AutoMapper Entity to Dto Conversion: Mapping Type Error

What is the best way to convert entity to dto? For Spring REST API Application

Spring boot: Custom mapping between DTO and Entity using ModelMapper

map inherited DTO to Entity

Spring jpa repository returns the entity class instead of the dto interface

Stats in entity response DTO

Converting DTO to Entity Manually

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive