Git Product home page Git Product logo

Comments (3)

spring-projects-issues avatar spring-projects-issues commented on May 2, 2024 8

Kamil commented

For our project is important to use Specification together with DTO created in JPQL.

Is there any possibility it will be supported? I see that this issue have a lot of votes...

from spring-data-jpa.

yasslkb avatar yasslkb commented on May 2, 2024 1

we need this

from spring-data-jpa.

ilkou avatar ilkou commented on May 2, 2024

in Hexagonal Architecture for example we have a domain class and a jpaEntity class;

  • the domain class could be:
class User {
   private String id;
   private String fullname;
   private String phone;
   private String stuff;
   ...
}

and

class Wallet {
   private String id;
   private String balance;
   private User user; // we could have a constructor (lite modal) with only fullname / id
   ...
}
  • the corresponding jpa entities could be:
class UserJpaEntity {
   private String id;
   private String fullname;
   private String phone;
   private String stuff;
   ...
}

and

class WalletJpaEntity {
   private String id;
   private String balance;
   private String userId;
   ...
}

now due to Hexagonal Architecture, we can't just use @ OneToOne or such annotations to link jpa entities and call it a day, I think..

Let's tackle the problem;

To fetch List/Page of Wallets for example, with their Users, one might fetch all wallets then loop through them and fetch the User, and that sounds like n+1 problem 🤔

or we can simply make a custom Wallet constructor and use it to map the result with @ Query, like the following:

@Query("""
        SELECT new io.spring.demo.Wallet(w.id, w.balance, u.id, u.fullname) FROM WalletJpaEntity w
        INNER JOIN UserJpaEntity u ON u.id = w.userId
        """)
Page<Wallet> findWallets(Pageable pageable);

and this works 🎉

But...

For filtering for example, we would need to use Specification API (that's why I'm commenting on this Issue), like this:

@Query("""
        SELECT new io.spring.demo.Wallet(w.id, w.balanace, u.id, u.fullname) FROM WalletJpaEntity w
        INNER JOIN UserJpaEntity u ON u.id = w.userId
        """)
Page<Wallet> findWallets(Specification<WalletJpaEntity> spec, Pageable pageable);

which doesn't work currently....

any work around this to assure we can have dynamic filters + efficient queries with joins (custom result mapping) at the same time ?

PS: A StackOverFlow question with same issue

from spring-data-jpa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.