JPA: direct mapping of collection size to attribute using count?

Class A has a one-to-many relationship with B Therefore, A has the collectionOfB attribute.

Is it possible to match “score B” with one attribute in A ?

The goal is to offer a shortcut to retrieve the number of associated B without loading the entire collection. Sometimes I only need an account, i.e. Collection size. I know that I can fulfill a request regarding an entity manager who does just that. However, seeing that the JPA provider did this, thanks to the comments, of course, would be preferable.

+4
source share
3 answers

I managed to solve my problem using HL-Lite: laql = "nofollow"> http://www.frightanic.com/2010/11/21/extra-lazy-one-to-many-mapping-with-hibernate/

+3
source

As far as I know, this is not possible. As you said, run a query to achieve this, which should not be expensive unless you have hundreds of thousands of B objects, and you can still cache it and update it every x minutes.

A more error prone solution is to have a statistics object with these types of numbers.

0
source

All Articles