I believe what you want is to investigate the @Formula
annotation. It simply drops in whatever snippet you put in there as part of the select clause for that column, and makes that property read-only. (@ColumnTransformer
is the same idea except it supports two-way functions.)
Something like the below should work:
@Formula("(SELECT COUNT( score ) + 1 FROM users u WHERE u.score > ( SELECT score FROM users u2 WHERE u2.userId = id))")
Integer rank;
Note: the id is not qualified, and hibernate will generate the alias prefix for the table of User class.
Not a particularly well-documented feature of Hibernate. But see: http://docs.jboss.org/hibernate/core/4.3/manual/en-US/html/ch05.html#mapping-declaration-property - 5.1.4.1.5. Formula.
You might look at http://www.jroller.com/eyallupu/entry/hibernate_derived_properties_performance_and.
You can make it lazy too in the normal way.
Alternative
You could also make a users view which has this sql embedded in it. This might be valuable in some cases, but will make your entity read-only. (Some databases do support DML against views, but I can't say how well that would work in this case, and you asked for a Hibernate based solution.)