Community
Participate
Working Groups
Build Identifier: 2.2.0.v20110202-r8913 Let's say I have the following entity class and embedded-id classes: @Entity public class CityZip implements Serializable { @EmbeddedId protected CityZipPK cityZipPK; @Column private String county; } @Embeddable public class CityZipPK implements Serializable { @Basic(optional = false) @Column(name = "zip") private String zip; @Basic(optional = false) @Column(name = "city") private String city; @Basic(optional = false) @Column(name = "state") private String state; } My JPA query is: "SELECT DISTINCT o FROM CityZip o WHERE o.cityZipPK.city = :city ORDER BY o.cityZipPK.state, o.cityZipPK.city, o.cityZipPK.zip" Everything works fine in SQL Server 2000, but SQL Server 2005 throws the following error: Server: Msg 209, Level 16, State 1, Line 1 Ambiguous column name 'state'. Server: Msg 209, Level 16, State 1, Line 1 Ambiguous column name 'city'. Server: Msg 209, Level 16, State 1, Line 1 Ambiguous column name 'zip'. Tracing the query in SQL Manager reveals the following SQL statement: declare @p1 int set @p1=NULL exec sp_prepexec @p1 output,N'@P0 nvarchar(4000)',N'SELECT DISTINCT city, County, state, zip, state, city, zip FROM cityzip WHERE (city = @P0) ORDER BY state ASC, city ASC, zip ASC ',N'New York' select @p1 So, the problem is the repeat of city, state and zip in the SELECT clause, causing the ORDER BY to get confused. This appears to be a problem with using DISTINCT. MS SQL Server 2000 doesn't complain with the resulting SQL statement, but MS SQL Seerver 2005 does. Reproducible: Always Steps to Reproduce: Test with the following: @Entity public class CityZip implements Serializable { @EmbeddedId protected CityZipPK cityZipPK; @Column private String county; } @Embeddable public class CityZipPK implements Serializable { @Basic(optional = false) @Column(name = "zip") private String zip; @Basic(optional = false) @Column(name = "city") private String city; @Basic(optional = false) @Column(name = "state") private String state; } SELECT DISTINCT o FROM CityZip o WHERE o.cityZipPK.city = :city ORDER BY o.cityZipPK.state, o.cityZipPK.city, o.cityZipPK.zip
Setting target and priority. See the following page for the meanings of these fields: http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines Community: Please vote for this bug if it is important to you. Votes are one of the main criteria we use to determine which bugs to fix next.
This issue is that the order by is on an embedded, so although EclipseLink checks to see if the field is already in the select, it doesn't find it, so it gets duplicated. This is normally not an issue, but seems to be on SQL Server 2005. The only reason we add the order bys to the select in the first place is because Oracle requires it (but only if a join is used).
I will fix it to check for aggregates. Also I will add shouldSelectDistinctIncludeOrderBy() to the platform so platforms such as SQL Server which may not requires this (I think) will not need to do it.
See, Bug#314025
Hi, we're migrating our project from toplink essentials to Eclipselink 2.2.0.v20110202-r8913 (version bundled with last release of Glassfish). We found that the same bug is replicable with Oracle 11g too!
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink