This week's book giveaway is in the Go forum.
We're giving away four copies of Head First Go and have Jay McGavren on-line!
See this thread for details.
Win a copy of Head First Go this week in the Go forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Spring Boot: get count without loading all  RSS feed

 
Greenhorn
Posts: 19
Java jQuery PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey! I have my user entity setup like this:



And my /users controller setup like this:


This works. However, I'd like another property in "User", called "followersAmount". However, calling "User#getFollowers().size()" doesn't cut it, as it can be quite a lot of followers (1 million+), and I don't want to load them all (eager). Is there a way in Spring boot that can achieve that?
 
Sheriff
Posts: 21653
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, JPA doesn't support performing custom queries on entity methods. I understand that getFollowers().size() is not something you want to do, because that will indeed load all followers. I'd therefore go for an extra method on my repository that performs a count on the user entity table. Your input should then be either the full user object, or only its id. It does mean that your code will have to call a method on the repository instead of the user.
 
Marius Richardsen
Greenhorn
Posts: 19
Java jQuery PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:As far as I know, JPA doesn't support performing custom queries on entity methods. I understand that getFollowers().size() is not something you want to do, because that will indeed load all followers. I'd therefore go for an extra method on my repository that performs a count on the user entity table. Your input should then be either the full user object, or only its id. It does mean that your code will have to call a method on the repository instead of the user.



Yes! And this is indeed something which I have done:


And in my user-service, which has auto-wired the JPA repository:


However, the issue is with this, is that I have to wrap everything with that UserService#loadPublic(User user)-- and it gets even more complicated when I want to fetch a following-relationship (Follower.class), which you know, has the "User" object inside and not the "PublicUser".


Are there no design patterns or Spring designs that allows for me to easily achieve this? I thought of maybe creating an User interface, which has all of the "public" information, then just have the User entity implement that, in which case all I have to call is:




However, then again, how do I get the amount of followers from within the User entity class? It's not like I can inject my followerService there, as it's not supposed to be a singleton.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!