Described Implicit Join generates "CROSS JOIN" instead of "INNER JOIN"

Description

We have the following Entities defined:

@Entity @Table(name = "table1") public class Table1 implements VersionSupport, Persistable<Integer> { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Basic(optional = false) @Column(name = "table1_id", nullable = false) private Integer table1Id; @Basic(optional = true) @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "table2_refid") private Table2 table2; } @Entity @Table(name = "table2") @Indexed public class Table2 implements Persistable<Integer>, Auditable, VersionSupport { @Id @GeneratedValue @DocumentId @Column(name = "table2_id", nullable = false) private Integer table2Id; @Field @Column(name = "matchcode", nullable = false) private String matchcode; }

(shortend of course)

If I try to do a HQL-Query like the following, I get a "CROSS JOIN" instead of a "INNER JOIN":

StringBuilder sb = new StringBuilder(); sb.append("from Table1 where table2.matchcode = :refid"); Query query = sessionFactory.getCurrentSession().createQuery(sb.toString()); query.setInteger("refid", value); List<Table1> table1List = (List<Table1>) query.list(); return table1List; }

HQL then generates me the following sql:

select ... from Table1 t1 CROSS JOIN Table2 t2 where t1.table2_id = t2.id and matchcode = ...

instead of

select ... from Table1 t1 INNER JOIN Table2 t2 on t1.table2_id = t2.id where matchcode = ...

which I will get if I do a explicit join:

StringBuilder sb = new StringBuilder(); sb.append("from Table1 t1 join t1.table2Id where table2.matchcode = :refid"); Query query = sessionFactory.getCurrentSession().createQuery(sb.toString()); query.setInteger("refid", value); List<Table1> table1List = (List<Table1>) query.list(); return table1List; }

That happens although documentation tells me in different way:
http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/queryhql.html#queryhql-joins-forms

HQL supports two forms of association joining: implicit and explicit. The queries shown in the previous section all use the explicit form, that is, where the join keyword is explicitly used in the from clause. This is the recommended form. The implicit form does not use the join keyword. Instead, the associations are "dereferenced" using dot-notation. implicit joins can appear in any of the HQL clauses. implicit join result in inner joins in the resulting SQL statement. from Cat as cat where cat.mate.name like '%s%'

Who can help me in this case? I don't want to get ANY CROSS JOIN in my Program. In this case I have mapped my refid as nullable as it has to be filled later on in my program. But at the time I create this object it is nullable as I do not have this Object.

Either way -> I turned it to not null and it happened again...

Activity

Steve EbersoleNovember 2, 2015 at 2:52 PM

No problem. In the meantime I am going to close this as out-of-date.

CNovember 2, 2015 at 7:42 AM
Edited

Hi, we still use Version 4.2.18 and I see no option to Update to 5.x in short term.
: Maybe, I will talk to my manager if we could try an update to verify this...

Steve EbersoleOctober 30, 2015 at 12:19 PM

Unless we hear back from OP I tend to agree with 's assessment here. The idea is simply that the inner join is semantically what happens. How that happens depends on the Dialect and where/how the SQL is being generated. The last part is because we actually (currently) generate SQL differently in multiple situations. That is being actively fixed.

Christian BeikovOctober 30, 2015 at 8:01 AM

Any chance that this is related to HHH-9305 as fixed in 4.1.16.Final? http://in.relation.to/2014/11/03/hibernate-orm-437-final-and-4216-final-released/

Steve EbersoleOctober 28, 2015 at 3:26 AM

As part of verifying that this issue affects 5.0, please just set the "Affects version". Leave the "verify-affects-5.0" label and leave the issue in "Awaiting Response" status; these are critical for us to be able to track these verifications and triage them. Thanks.

Out of Date

Details

Assignee

Reporter

Labels

Components

Affects versions

Priority

Created October 22, 2012 at 3:35 PM
Updated November 2, 2015 at 2:52 PM
Resolved November 2, 2015 at 2:52 PM

Flag notifications