I am working on an application in which there are users with regular fields such as userId, name etc., additionally users also have score and rank. rank is calculated on request, depending on the score of the user. it actually is the order of the user in the whole system, the user's rank with highest score is 1.
I am using hibernate 4 for orm and mysql 5.1... for db ; my User class is as follows, as I said earlier rank is not a column in the db;
@Entity
@Table(name = "users")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(insertable = false, updatable = false)
Integer userId;
@Column
String name;
@Column
Integer score;
Integer rank; // rank is calculated according to score
...
here is the sql statement to retrive the order of the user;
SELECT COUNT( score ) + 1 as rank
FROM users
WHERE users.score > ( SELECT users.score
FROM users WHERE `users`.`userId` = :uid)
I can also use stored db function or procedure.
Is it possible to retrieve user's rank on each request as if it is nested, along with other attributes of the user entity?
Solutions using annotations is much appriciated.