Dirty Entity instances in session after HQL queries with where conditions on OneToMany relation

Description

Having a OneToMany relation on Team.persons, after you execute a HQL query in the form

"select distinct t from Team t join fetch t.persons p where p.id = ?"

the selected Team instance only contains the one person with the id searched for (i.e. missing other persons in this team).

Probably this could be worked around by changing the query, the main problem ist that this broken Team instance is attached in the session and querying for the team by it's id returns this broken team:

Team newTeamFetchedById = session.get(Team.class, TEAM_ID); // team id from the query above

Evicting the fetched team and repeating the query again fetches a team with the correct number of persons.

Environment

Hibernate 3.3.2.GA, Java 1.6

Activity

Show:
Gail Badner
September 13, 2010, 11:50 PM

This is expected behavior.

Felix von Delius
September 16, 2010, 9:57 AM
Edited

> This is expected behavior.

Huh? When executing a query that starts with "select from Team t join fetch t.persons", I would expect to get the team with all it's members, independent of the where clause. And even if this is a idiosyncrasy of this specific query, executing another query in the same session where the team is selected by id, I would expect to get the complete team.

Btw: leaving out the "fetch" in the above query seems to return the team with all members.

If this is really the correct, expected behaviour, where is this "expected" behaviour documented?

Executing the same query as a criteria query does have the (IMHO) expected behaviour (returning the team with all it's members):

Criteria crit = session.createCriteria(Team.class);
crit.createAlias("persons", "p");
crit.add(Restrictions.eq("p.id", teamMember.getId()));
List<Team> teamList = crit.list();

Steve Ebersole
March 21, 2011, 7:08 PM

Bulk closing stale resolved issues

Assignee

Gail Badner

Reporter

Felix von Delius

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure