Search code examples
springhibernatejpaspring-dataquery-by-example

JPA QBE with @JoinTable


I am using the recently released QBE functionality through spring-data-jpa. Everything seems to work just fine, with the one exception being that fields annotated as @JoinTable do not seem to work. I've tried simple primitives on the entity, as well as a @JoinColumn relationship, and the query performs as expected. However, once I tried to populate a Set of objects that were a @JoinTable relationship, that data does not become part of my query (I checked the SQL that is logged). We are using Hibernate as our JPA provider, so perhaps the issue is with the implementation. I'm just going to assume I'm doing something wrong for now.

@ManyToMany
@JoinTable(name = "join_table", joinColumns = @JoinColumn(name = "userid", nullable = false), inverseJoinColumns = @JoinColumn(name = "groupid", nullable = false))
private Set<Group> groups= new HashSet<>(0);

That's the annotated field that is not working. In order to populate it for my QBE I simply create a new group with a specific name I am looking for. I need the user to belong to that group in order to return. However, the SQL that is generated does not include anything about the group in the where clause.

Example<User> example = Example.of(user);

return userRepo.findAll(example, pageable);

And the snippet that calls the JPA repository.

Any ideas would be greatly appreciated.


Solution

  • At this time the QBE feature of Spring Data JPA only supports SingularAttributes. Therefore @JoinTable cannot be used. Please have a look at the "Executing an Example" section in reference manual.