views:

49

answers:

3
  1. How do you track the number of times a particular entity (say a user profile much similar to stackoverflow.com) is viewed by other users? Would it make sense to update this information when an entity is viewed using a lifecycle event like PostLoad.

  2. Where would you store the number of times the entity has been viewed, would it be in the entity table itself? (Assumption: The system doesn't require you to track the users who actually viewed a particular profile)

Note: The underlying persistence engine is based on JPA/Hibernate

A: 

I don't know the JPA/Hibernate, but basic logic would say: When someone loads the view, simply update the database to viewed+1.

Yossi
+1  A: 
For Question 1:

I would make a property in that entity class and increment it when ever that entity is accessed.

For Question 2:

Yes, I would store it in the same table as the entity.

Thanks

Mahesh Velaga
+1  A: 
  • createa a field (column) profileViews
  • update the field in an UI event, not a persistence event. So whenever the page with url=user.jsp?userId=12345 is opened, update the field. Updating it in PostLoad will lead to many unexpected and unwanted results. The User object is also loaded when the list of users is viewed, but this should not increase the number of views.
Bozho