title: Hibernate - Using Distinct Select query in Criteria
body: <p>Hope all of you are fine. Well I am quite new to all this Hibernate stuff and I am having a difficulty getting a Select Distinct query to work. The Query I'm trying to work is as follows, </p>
  
  <pre><code>Select Distinct user_type from Users
  </code></pre>
  
  <p>The users table has values as follows, </p>
  
  <pre><code>---------------------------------------
  | user_id  |  user_name  | user_type  |
  ---------------------------------------
  |    1     | mark taylor | admin      |
  |    2     | bill paxton |co-ordinator|
  |    3     | tony brooks | admin      |
  |    4     | ali jahan   | developer  |
  ---------------------------------------
  </code></pre>
  
  <p>The result I'm expecting at the end of the query is, </p>
  
  <pre><code>admin,co-ordinator,developer
  </code></pre>
  
  <p>What I'm hoping to get is a list of user_types as a return statement, the function i've written after searching for help is as follows, </p>
  
  <pre><code>public List&lt;User&gt; findDistinctUserType() throws HibernateException {
      Criteria c = this.createCriteria();
      c.setProjection(Projections.distinct(Projections.property("user_type")));
      return (List&lt;User&gt;) (User) c.uniqueResult();
  }
  </code></pre>
  
  <p>I've looked at another example and tried this as well,</p>
  
  <pre><code>public List&lt;User&gt; findDistinctUserType() throws HibernateException {
      Criteria c = this.createCriteria();
      ProjectionList projectionList = Projections.projectionList();
      ProjectionList projectionList2 = Projections.projectionList();
  
      projectionList2.add(Projections.distinct(projectionList
              .add(Projections.property("user_type"),"user_type")));
  
      c.setProjection(projectionList2);
      c.setResultTransformer(Transformers.aliasToBean(User.class));
  
      return (List&lt;User&gt;) (User) c.uniqueResult();
  }
  </code></pre>
  
  <p>But for odd reason, it's always failing and I'm not getting through, if you could please provide me with a good solution, that'll be great. Looking forward to your help. </p>
  
  <p>Thanks in advance </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The output I am expecting is as follows, </p>
  
  <pre><code>admin,co-ordinator,developer
  </code></pre>
  
  <p>What I am currently getting without this code is, </p>
  
  <pre><code>admin,co-ordinator,admin,developer
  </code></pre>
  
  <p>The above mentioned function is called in the following manner, </p>
  
  <pre><code>List&lt;User&gt; userList = userDao.findDistinctUserType();
  </code></pre>
  
  <p>User is an object which contains the following information, </p>
  
  <pre><code>private int id;
  private String userName;
  private String userType;
  </code></pre>
  
  <p>further down the code we have where User object is defined, we have, </p>
  
  <pre><code>@Column(name="user_type", nullable=false)
  public String getUserType() {
      return userType;
  }
  </code></pre>