I have two tables that do not have a specific relationship, but I'm still trying to combine them into one query using the build criteria API.
This query works the way I want to synchronize rows from both tables:
Root<E_Application> root = q.from(E_Application.class); Root<E_Searcher> root2 = q.from(E_Searcher.class); q.where(cb.equal(root.get(E_Application_.packageName), root2.get(E_Searcher_.packageName))); q.select(cb.sum(cb.literal(1)));
the query that comes out: select sum(1) from application t0 cross join application_searcher t1 where (t0.package_name = t1.package_name);
However, if I add another connection:
Root<E_Application> root = q.from(E_Application.class); Root<E_Searcher> root2 = q.from(E_Searcher.class); Join<E_Application, E_AppState> j1 = root.join(E_Application_.publicAppState); q.where(cb.equal(root.get(E_Application_.packageName), root2.get(E_Searcher_.packageName))); q.select(cb.sum(cb.literal(1)));
I get an extra cross join and end with a Cartesian product: SELECT SUM(1) FROM application t0 CROSS JOIN application t1 CROSS JOIN application_search t3 INNER JOIN application_state t2 ON t1.PUBLICAPPSTATE_ID = t2.id WHERE (t0.package_name = t3.package_name)
Is there a way to prevent this (with the exception of determining the correct relationship between Application and Searcher)? Is this the correct implementation of the specification? It is strange that as a result of the request, there is a root that I did not explicitly request, and I have no control over ...
The database is postgres, if that matters.
PS The reason for the lack of relationships is that these two tables have the same PK, and you cannot have PK, which is also an entity reference.
source share