Automatic flush to the join table before a criteria query

Description

With the default flush mode Hibernate flushes query regions before queries to run SQL against actual table data.
If two entity are connected using the join table and data is queried using a criteria Hibernate does not flush to the join table.
It flushes to the entity tables though.

The attached test case produces the SQL output

Hibernate: insert into USERS (ID, NAME) values (null, ?)
Hibernate: insert into ITEMS (ID, NAME) values (null, ?)
Hibernate: select this_.ID as ID0_1_, this_.NAME as NAME0_1_, items3_.USER_ID as USER1_, items1_.ID as ITEM2_, items1_.ID as ID2_0_, items1_.NAME as NAME2_0_ from USERS this_ inner join USER_ITEMS items3_ on this_.ID=items3_.USER_ID inner join ITEMS items1_ on items3_.ITEM_ID=items1_.ID where items1_.NAME=?

But it should flush to the USER_ITEMS table before executing the criteria query.

The commented HQL query does flush to both the entity tables and the join table.

The problem is in the org.hibernate.loader.criteria.CriteriaQueryTranslator.getQuerySpaces() method.
It returns only the entity tables.

Environment

Hibernate Core 3.3.1.GA, H2 1.1.108

Activity

Show:
Etienne Miret
February 7, 2015, 5:28 PM

I made two pull requests that fixes this bug thanks to the patch provided by :

  • PR 885 fixes it on branch 4.2.

  • PR 886 fixes it on branch 4.3.

Stefan Schedl
March 23, 2015, 10:17 PM

@Etienne Miret, thank you very much. I tested your patch with my test cases and it works fine.

Is there any chance to get it fixed in the next bugfixing version?

Etienne Miret
March 24, 2015, 7:33 PM

@ It’s not my patch, it’s Maxim’s. I only added some tests and created the pull request.

Regarding the inclusion of this fix in the next release, well it depends on when or someone else with write access to the repository will find time to review it. Which can be very long. The current oldest pull request waiting for approval is over one year old.

Regards,

Guillaume Couture-Levesque
March 25, 2015, 6:16 PM

Given that there's already fixes for this, testcases that show the bug, and it's a pretty major bug, what's the holdup?

Vlad Mihalcea
March 19, 2018, 5:10 PM

Applied PR upstream and on 5.2.

Fixed

Assignee

Unassigned

Reporter

Maxim Gordienko

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure