含WHERE连接条件的CROSS JOIN SQL是否为真交叉连接?
Great question—this is a super common point of confusion when digging into auto-generated SQL from ORMs like Hibernate. Let’s break this down clearly:
Short Answer
While the syntax uses CROSS JOIN keywords, this query is logically equivalent to an INNER JOIN and will not produce a true Cartesian product (the "real" unfiltered CROSS JOIN behavior).
Detailed Breakdown
Syntax vs. Actual Execution
A true CROSS JOIN returns every possible combination of rows from the joined tables (no filtering or joining conditions). But here, theWHEREclause includes critical implicit join conditions:c.targetsystempublication = t.idc.canonicalitem = m.id
Database optimizers immediately recognize these as join conditions, so they won’t waste resources generating a full Cartesian product first. Instead, they’ll execute this query exactly like an INNER JOIN with explicitONclauses.
Hybrid of ANSI-89 and ANSI-92 Syntax
This query mixes two join syntax standards:- The
CROSS JOINkeyword comes from the newer ANSI-92 standard - The join conditions tucked into the
WHEREclause are classic ANSI-89 style (before explicitJOIN ... ONbecame the norm)
Hibernate sometimes generates this hybrid when translating entity associations into SQL, especially if the mapping doesn’t explicitly define join types using modern syntax.
- The
Equivalent Modern INNER JOIN Query
To make this even clearer, here’s the exact same logic rewritten using standard ANSI-92 INNER JOIN syntax—this will produce identical results and execution plans:SELECT max(c.item) FROM ItemPubStatus c INNER JOIN TargetSystemPub t ON c.targetsystempublication = t.id INNER JOIN Item m ON c.canonicalitem = m.id WHERE t.targetsystem = ? AND m.datapool = ? AND m.itemtype = ?
Why Hibernate Generates This?
It usually comes down to how your entity mappings are set up. If Hibernate doesn’t detect an explicit join definition (like a @JoinColumn with a clear association), it may fall back to this syntactic quirk. Rest assured, though—this isn’t a bug, and the query will perform exactly like a standard INNER JOIN.
内容的提问来源于stack exchange,提问作者Justin




