I have two classes:
public class ComicBook {
@OneToOne(mappedBy = "comicBook", cascade = CascadeType.ALL, orphanRemoval = true)
private ComicMetadataSource metadata;
}
and:
public class ComicMetadataSource {
@OneToOne
private ComicBook comicBook;
}
I'm trying to use CriteriaBuilder to programmatically create a query to load records from ComicBook that do not have an associated ComicMetadatSource record. Specifically, I tried this:
final CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
CriteriaQuery<ComicBook> criteriaQuery = criteriaBuilder.createQuery(ComicBook.class);
criteriaQuery.distinct(true);
Root<ComicBook> comicBook = criteriaQuery.from(ComicBook.class);
if (unscraped) {
comicBook.join("metadata");
comicBook.get("metadata").isNull();
}
return query.getResultList();
However, this is returning records from ComicBook that definitely have a ComicMetadataSource record attached to them.
I've avoided CriteriaBuilder because of the complexity when working with multiple tables, but I'm in a situation where the best solution is to use criteria, but it's really confusing me how to work across tables like this.