Hibernate generates in for some DBMS wrong SQL in special cases. In my case, i have an Spring application that querys an table with an collection of an embeddable key with two fields and one additional boolean value (similar as in-List with tuples - which is not supported by DB2). In this case, the generated SQL returns all rows that matches the embeddable expect for the last - for this the row must match the embeddable and the boolean value. Example Table-Definition
Data
Query-Method (Spring Data JPA) public List<TestEntity> findAllBySubInAndPresent(Collection<Sub> subs, String present); Model
generated wrong SQL: {{select t1_0.id,t1_0.present,t1_0.key_1,t1_0.key_2 from tester t1_0 where (t1_0.key_1=? and t1_0.key_2=?) or (t1_0.key_1=? and t1_0.key_2=?) or (t1_0.key_1=? and t1_0.key_2=?) and t1_0.present=? }} Which returns the rows with the IDs: 1, 3 and 4. Expected are only the rows with the IDs: 1 and 4 This is fixed by adding parenthesis around the ORs of the Collection-Data: select t1_0.id,t1_0.present,t1_0.key_1,t1_0.key_2 from tester t1_0 where ((t1_0.key_1=? and t1_0.key_2=?) or (t1_0.key_1=? and t1_0.key_2=?) or (t1_0.key_1=? and t1_0.key_2=?)) and t1_0.present=? This incorrect SQL generation only occours for DBMS that do not support the supportsRowValueConstructorSyntaxInInList. For example DB2. This problem occurs in versions 5.x and 6.x. I've allready fixed this issue and i'll submit an PR the next hours. |