I’ve got a simple web application with two persistent classes, User and Post, both managed by Hibernate. I’d like the User to be able to list the posts which he hadn’t yet read. How can I implement this functionality using Hibernate?
I’ve got a simple web application with two persistent classes, User and Post, both
Share
Add a UserPost entity that contains a User and a Post
Select p from Post p where p not in ( select up.post from UserPost up where up.user.id = :userId )I’m sure there is a more performance friendly query than
not inbut you get the gist.Another option is to add a collection of Posts to the User and give it a lazy fetch type but I’m assuming that collection will likely get large and adding the association merely for the query doesn’t seem great to me.
Adding a separate entity for the join table will keep you from having a direct association from the User to the Post or vice versa. When the user reads a post, you add a record to the UserPost table. You could also embellish the entity with a date to store when they read the entry, or a count so you can keep track of their most popular posts or whatever.