Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA: directly mapping collection size to attribute using count?

Class A has a one-to-many relationship to B. Hence, A has an attribute collectionOfB.

Is there any way I could map "count B" to single attribute in A?

The purpose would be to offer a shortcut to retrieve the number of associated Bs without loading the entire collection. Sometimes all I need is the count i.e. the collection size. I know I could issue a query against the entity manager that does exactly that. However, seeing it done by the JPA provider thanks to annotations would of course be preferable.

like image 805
Marcel Stör Avatar asked Nov 19 '10 23:11

Marcel Stör


1 Answers

I was able to solve my problem using Hibernate's extra-lazy mapping: http://www.frightanic.com/2010/11/21/extra-lazy-one-to-many-mapping-with-hibernate/

like image 71
Marcel Stör Avatar answered Sep 20 '22 06:09

Marcel Stör