| Summary: | Bad SQL result for method countDistinct with join from criteria API | ||
|---|---|---|---|
| Product: | z_Archived | Reporter: | Radek Hodain <radek.hodain> |
| Component: | Eclipselink | Assignee: | Nobody - feel free to take it <nobody> |
| Status: | NEW --- | QA Contact: | |
| Severity: | normal | ||
| Priority: | P2 | CC: | eclipselink.orm-inbox, gordon.yorke, tom.ware, virgo47 |
| Version: | unspecified | ||
| Target Milestone: | --- | ||
| Hardware: | PC | ||
| OS: | Linux | ||
| Whiteboard: | |||
|
Description
Radek Hodain
I have not been able to recreate this issue. Please post the code for User, Group and UserGroupAsoc and any xml files that contribute to how they are mapped. Deferring because I cannot recreate the exact error described here. Note: I do get some issues creating these kinds of queries, but any change I implemented could not be verified to fix this bug, so I am deferring until more information is available. Just today I hit a bug with EclipseLink that seems to be related both to this bug and this thread http://www.eclipse.org/forums/index.php/t/170219/ The trouble seems to be with the combination of DISTINCT and COUNT *with* the composite PK. For instance JPQL for entity with simple PK: select distinct security from Security security inner join security.domains as cd on cd.id = ?1 Is transformed properly into: SELECT COUNT(DISTINCT(t0.id)) FROM Securities t0, Securities_Domains t2, Domains t1 WHERE (((t2.security_id = t0.id) AND (t1.id = t2.domain_id)) AND (t1.id = ?)) But virtually equivalent JPQL - but this time with "subInstrument" having composite PK (two Integers): select count(distinct subInstrument) from SubInstrument subInstrument inner join subInstrument.domains as cd on cd.id = ?1 Turns into this - no join to domains at all: SELECT COUNT(t0.siid) FROM SubInstruments t0 WHERE EXISTS (SELECT t1.siid FROM SubInstruments t1 WHERE ((t0.siid = t1."siid") AND (t0."iid" = t1."iid"))) While the forum thread mentioned before says that DISTINCT and COUNT doesn't go together well, Hibernate has this solved so it seems: https://hibernate.atlassian.net/browse/HHH-3096 The DISTINCT cannot be omitted as the relationship to domains is *ToMany. It should be reproducible with the following setup: @Entity public class Role { @Id private Integer id; } @Entity public class Right { @EmbeddedId private RightPk id; @ManyToMany private Set<Role> roles; } @Embeddable public class RightPk { @Column private Integer id1; @Column private Integer id2; } And JPQL like this: select count(distinct r) from Right r inner join r.roles as rr on rr.id in (?1) I have to admit that this seems not to be the bug after all. In JPA specification, at least in 2.1 version, section 4.8.5: The use of DISTINCT with COUNT is not supported for arguments of embeddable types or map entry types. Shame it's not, but it is specified. The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink |