Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using <code>@SuppressWarnings</code> everywhere, as suggested, is a good way to do it, though it does involve a bit of finger typing each time you call <code>q.list()</code>.</p> <p>There are three other techniques I'd suggest:</p> <p><strong><code>Collections.checkedList()</code></strong></p> <p>Replace your assignment with this:</p> <pre><code>List&lt;Cat&gt; cats = Collections.checkedList(q.list(), Cat.class); </code></pre> <p>You might want to check the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html" rel="nofollow noreferrer">javadoc for that method</a>, especially with regards to <code>equals</code> and <code>hashCode</code>.</p> <p><strong>Write a cast-helper</strong></p> <p>Simply refactor all your <code>@SuppressWarnings</code> into one place:</p> <pre><code>List&lt;Cat&gt; cats = MyHibernateUtils.listAndCast(q); ... public static &lt;T&gt; List&lt;T&gt; listAndCast(Query q) { @SuppressWarnings("unchecked") List list = q.list(); return list; } </code></pre> <p><strong>Prevent Eclipse from generating warnings for unavoidable problems</strong> </p> <p>In Eclipse, go to Window>Preferences>Java>Compiler>Errors/Warnings and under Generic type, select the checkbox <code>Ignore unavoidable generic type problems due to raw APIs</code></p> <p>This will turn off unnecessary warnings for similar problems like the one described above which are unavoidable.</p> <p>Some comments:</p> <ul> <li>I chose to pass in the <code>Query</code> instead of the result of <code>q.list()</code> because that way this "cheating" method can only be used to cheat with Hibernate, and not for cheating any <code>List</code> in general.</li> <li>You could add similar methods for <code>.iterate()</code> etc.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload