The attachment is not a fix but can be run against Hibernate 5.1.0 or 5.0.9 to create a testcase. The test added will not fail with an error. I enabled logging to see the incorrect queries (i.e. not using subselect).