SQL join is missed in Criteria queries for join/alias on foreign key as part of composite primary key (many-to-one association)

Description

I've stuck with Criteria API on issue, that the join is sometimes missed from generated SQL query doing joining on foreign key.

Here is the little example I've made to demonstrate the problem (also attached as standalone runnable eclipse project)

There are two tables:
FAMILY
familyName as primary key
familyProperty

PERSON
familyName as foreign key on FAMILY } the both fields make primary key
personName } of PERSON

Doing query on person like
session.createCriteria(Person.class)
.createCriteria("id.family")
.add(Restrictions.eq("familyProperty", "whatever"))
.list();

Causes query
Hibernate: select this_.FAMILY_NAME as FAMILY1_1_0_, this_.PERSON_NAME as PERSON2_1_0_ from PERSON this_ where family1_.FAMILY_PROPERTY=?
.. which is rather invalid

The same by the way happens doing query
session.createCriteria(Person.class)
.createCriteria("id.family")
.add(Restrictions.eq("familyName", "whatever"))
.list();

The query
select this_.FAMILY_NAME as FAMILY1_1_0_, this_.PERSON_NAME as PERSON2_1_0_ from PERSON this_ where family1_.FAMILY_NAME=?
.. is invalid either though it can be made valid without join, because family1_.FAMILY_NAME can be accessed through this_.FAMILY_NAME

The attachment is a whole eclipse project, pretty straightforward, with hsqldb, which demonstrates the SQL generated and the problem.

I suspect this bug may be caused by the fix to HH-528 (component.manyToOne.id in HQL causes join).

The priority should be high because
session.createCriteria(Person.class)
.createAlias("id.family", "family")
.add(Restrictions.eq("family.familyProperty", "whatever"))
.list();
doesn't work either.
In effect there is no way to cause join on foreign key with Criteria API. Whatever in hibernate blocks join on foreign key rather zealous.

Attachments

1

Activity

Show:

Steve Ebersole March 21, 2011 at 7:04 PM

Bulk closing stale resolved issues

Diego Plentz September 11, 2007 at 7:07 AM

Closing this issue as dup because it is older then HHH-1570. Btw, it has more info.

Mattias Jiderhamn November 30, 2006 at 10:34 PM

After a bit of debugging it seems the problem lies within the JoinWalker (or specifically CriteriaJoinWalker). When creating a list of associations that may be used for joins (in walkEntityTree()) it only looks at the properties of the persister/OuterJoinLoadable (SingleTableEntityPersister in my case). The composite-id/key-many-to-one mappings are not listed as properties, and therefore not even considered for joins. So, when AbstractEntityJoinWalker creates the actual statement, it doesn't know about the relation to the "parent" (key-many-to-one).

Not sure yet what the correct change would be, but hopefully somebody could use the information above or I find the time to try something out.

Mattias Jiderhamn November 30, 2006 at 10:32 PM

Reported against 2.1

John Trollinger July 13, 2006 at 4:39 PM

This problem seems to be the same as HHH-1570. The composite key items tables are not added to the query.

Duplicate

Details

Assignee

Reporter

Components

Affects versions

Priority

Created May 11, 2006 at 3:22 PM
Updated March 21, 2011 at 7:04 PM
Resolved September 11, 2007 at 7:07 AM