How can I add a guard to a JPA manyToMany join

hibernate, java, jpa

Solution

I've solved this for the time being using the Hibernate specific @Where annotation. I just required an extra annotation on the user's pub collection like so:

@ManyToMany(mappedBy = "users")
@Where(clause="closed='false'")
private List<Pub> visited;

Of course I'm now locked in to Hibernate, which is not a huge problem for this project, but if anyone has a generic JPA solution I'd love to hear it.

Problem

My testing app for JPA tracks the number of pubs a user has visited in my town. I have this on the user object ``` @ManyToMany(mappedBy = "users") private List<Pub> visited; ``` And the other side on the pub object ``` @ManyToMany(cascade = CascadeType.ALL) @JoinTable( joinColumns = @JoinColumn(name = "pubid"), inverseJoinColumns = @JoinColumn(name = "userid")) protected Set<User> users; ``` However, I've updated the pub table with a column indicating that the pub has been closed. I only want the the user's visited List to contain active pubs so the question is How can I conditionally join these objects so that only pubs that pass a test (e.g. table.closed=false) will be put in the user's visited list? I'm using hibernate and postgres underneath.

Original source