title: Hibernate -> ArrayList cannot be cast to Set
body: <p>I have a Java EE application and I use Hibernate. The domain objects, I changed the List / ArrayList to Set / HashSet, because it is better to use Sets.</p>
  
  <p>But in my Dao implementation I run into a problem:</p>
  
  <pre><code>public Set&lt;Person&gt; getAllPersons() {
      SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
      Session sess = sessionFactory.getCurrentSession();
  
      Transaction tx = sess.beginTransaction();
      @SuppressWarnings("unchecked")
      Set&lt;Item&gt; items = (Set&lt;Item&gt;) sess.createQuery("from Item").list();
      tx.commit();
  
      return items;
  }
  </code></pre>
  
  <p>Here I get an error:</p>
  
  <pre><code>java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Set
  </code></pre>
  
  <p>What can I do to avoid this error?</p>
  
  <p>Thank you in advance &amp; Best Regards.</p>