title: Spring Data JPA Interface and class based projection not working for DISTINCT field of Embedded key
body: <p>Following is my entity structure. EmployeeKey is an EmbeddedId(composite key) within Employee.</p>
  
  <p>This is the native query I want to implement using Spring-Data.</p>
  
  <pre><code>select DISTINCT(ID), NAME, DEPARTMENT from EMPLOYEE;
  </code></pre>
  
  <p>I am trying to use <strong>Interface based and Class based Spring data JPA projections</strong> but none of the approaches seem to work. Interface-based projections give List of proxies which I am unable to unproxy. Using <strong>DISTINCT</strong> in constructor of Class-based Projections is not possible AFAIK.</p>
  
  <pre><code>@Entity
  @Table(name = "EMPLOYEE")
  public class Employee {
     @EmbeddedId
     private EmployeeKey key;
  
     @Column(name = "NAME")
     private String name;
  
     @Column(name = "DEPARTMENT")
     private String department;
  
     @Coulmn(name = "AGE")
     private Integer age;
  
     //Getter/Setters/Constructors
  }
  
  @Embeddable
  public class EmployeeKey {
     @Column(name = "ID")
     String id;
  
     @Column(name = "REGNO")
     String regNo;
  
     //Getter/Setters/Constructors
  }
  
  @Repository
  public interface EmployeRepository extends JpaRepository&lt;Employee, EmployeeKey&gt;{
     @Query(value = "select distinct(emp.key.id), emp.name, emp.department from Employee emp")
     List&lt;EmployeeInterfaceProjection&gt; findUsingInterfaceProjection();
  
     @Query(value = "select distinct(emp.key.id) as empId, emp.name as empName, emp.department as empDepartment from Employee emp")
     List&lt;EmployeeClassProjection1&gt; findUsingClassProjection1();
  
     @Query(value = "select new com.path.to.EmployeeClassProjection2(emp.key, emp.name, emp.department) from Employee emp")
     List&lt;EmployeeClassProjection2&gt; findUsingClassProjection2();
  
     @Query(value = "select distinct(emp.key.id) as empId, emp.name as empName, emp.department as empDepartment from Employee emp")
     List&lt;Object[]&gt; findUsingObjectProjection();
  }
  
  public interface EmployeeInterfaceProjection{
     EmployeeKeyInterfaceProjection getKey();
     String getName();
     String getDepartment();
  
     interface EmployeeKeyInterfaceProjection{
        String getId();
     }
  }
  
  public class EmployeeClassProjection1{
     private String empId;
     private String empName;
     private String empDepartment;
  
     //Getters/Setters, Constructors, Hashcode, Equals
  }
  
  public class EmployeeClassProjection2{
     private EmployeeKey key;
     private String name;
     private String department;
  
     //Getters/Setters, Constructors, Hashcode, Equals
  }
  </code></pre>
  
  <p><strong>PROBLEMS FACED IN EACH APPROACH</strong></p>
  
  <p><strong>findUsingInterfaceProjection()</strong> </p>
  
  <p>This gives a <strong>List which I am unable to unproxy to get the value</strong>. Hibernate.unproxy/initialize doesn't help either. While streaming on the proxies and calling getters: getKey(), getName(), getDepartment() gives null for each.</p>
  
  <p><strong>findUsingClassProjection1()</strong></p>
  
  <p>This gives "No converter found capable of converting from type [org.springframework.data.jpa.repository.query.<strong>AbstractJpaQuery$TupleConverter$TupleBackedMap</strong>] to type [com.path.to.EmployeeClassProjection1]" error. </p>
  
  <p>I know in order to resolve this I need to use a parameterized constructor directly within the query. But doing so prohibits me from using <strong>DISTINCT</strong> in the constructor.</p>
  
  <p><strong>findUsingClassProjection2()</strong></p>
  
  <p>This approach actually gets the data and not the proxies but i need the distinct filtration. Can't use DISTINCT within the contructor in query.</p>
  
  <p><strong>findUsingObjectProjection()</strong></p>
  
  <p>This works fine but looks like a very crude approach. I was hoping to use the JPA projections.</p>