Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 346729 - @Embeddable and DISTINCT keyword causes duplicate column names in SQL SELECT clause
Summary: @Embeddable and DISTINCT keyword causes duplicate column names in SQL SELECT ...
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Linux
: P2 normal with 4 votes (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-20 12:21 EDT by John Manko CLA
Modified: 2022-06-09 10:33 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description John Manko CLA 2011-05-20 12:21:57 EDT
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
Comment 1 Tom Ware CLA 2011-06-09 10:04:10 EDT
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.
Comment 2 James Sutherland CLA 2011-10-31 15:17:51 EDT
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).
Comment 3 James Sutherland CLA 2011-10-31 15:18:58 EDT
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.
Comment 4 James Sutherland CLA 2011-11-16 11:19:27 EST
See, Bug#314025
Comment 5 Claudio De Sio Cesari CLA 2013-02-11 09:00:07 EST
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!
Comment 6 Eclipse Webmaster CLA 2022-06-09 10:33:46 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink