title: Spring query distinct on a entire object
body: <p>I'm using spring data and I made this query:</p>
  <pre><code>@Query(&quot;SELECT DISTINCT u.pk.fleet.fleetName FROM FleetHasUser u WHERE u.pk.user.username = ?1&quot;)
  List&lt;FleetName&gt; allFleetNameForUser(String username);
  </code></pre>
  <p>The aim of this query is find all FleetName for a specific user.
  This is the part of database schema interested in:
  <a href="https://i.stack.imgur.com/tvJPB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tvJPB.jpg" alt="enter image description here" /></a></p>
  <p>The <code>FleetHasUser</code> class:</p>
  <pre><code>@Entity
  @Table(name = &quot;fleet_has_user&quot;, catalog = &quot;dart&quot;)
  @AssociationOverrides({
      @AssociationOverride(name = &quot;pk.user&quot;, 
          joinColumns = @JoinColumn(name = &quot;id_username&quot;)),
      @AssociationOverride(name = &quot;pk.fleet&quot;, 
          joinColumns = @JoinColumn(name = &quot;id_fleet&quot;)) })
  public class FleetHasUser implements java.io.Serializable {
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      private FleetHasUserKeys pk = new FleetHasUserKeys();
      
      
      @EmbeddedId
      public FleetHasUserKeys getPk() {
          return pk;
      }
  </code></pre>
  <p>the <code>FleetHasUserKeys</code> class:</p>
  <pre><code>@Embeddable
  public class FleetHasUserKeys implements Serializable {
      
      private static final long serialVersionUID = 1L;
      protected User user;
      protected Fleet fleet;
  </code></pre>
  <p><code>Fleet</code> class:</p>
  <pre><code>@Entity
  @Table(name = &quot;fleet&quot;, catalog = &quot;dart&quot;)
  public class Fleet implements java.io.Serializable {
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      private Integer idFleet;
      private Ecu ecu;
      private String application;
      private Cubic cubic;
      private Integer power;
      private String euroClass;
      private String engineType;
      private Traction traction;
      private String transmission;
      private String note;
      private FleetName fleetName;
      @JsonIgnore
      private Set&lt;Car&gt; cars = new HashSet&lt;Car&gt;(0);
      @JsonIgnore
      private Set&lt;FleetHasUser&gt; fleetHasUsers = new HashSet&lt;FleetHasUser&gt;(0);
  </code></pre>
  <p>As you can see FleetName is a class. I tried without distinct and the list had duplicated elements, so before create a subquery I inserted distinct only for test and it worked. I didn't find information on google, but is it possible to use distinct on a object? How does it work(check the primary key fields)?</p>
  <p>Maybe may I even create a Spring data query like <code>findDistinctPkFleetFleetNameByPkUserUsername</code>?
  Thanks</p>