SetMaxResults() Not Working In Hibernate
<p>i am newbie in Hibernate and getting some errors using the <code>SetMaxResults()</code> function. This is my MySQL table data::</p>

<pre><code>1   J. C. Smell
2   J. C. Smell
3   J. C. Smell
4   J. C. Smell
</code></pre>

<p>This is the method to retrieve one record from 4 identical records.</p>

<pre><code>private Person findPerson(Session session, String name){
        Query query = session.createQuery("from Person p where p.name=:name");
        query.setParameter("name", name);
        query.setMaxResults(1);
        Person person = (Person)query.list();
        if(person == null)
            return null;
        return person;
    }
</code></pre>

<p>I expect to get one record  but according to the Exception am getting an ArrayList of records. The exception comes from this line 
 <code>Person person = (Person)query.list();</code> That line has the number <code>34</code> in my source code.This is the exception.</p>

<pre><code>java.lang.ClassCastException: java.util.ArrayList cannot be cast to chapter3.simple.Person
    at chapter3.simple.RankingTest.findPerson(RankingTest.java:34)
    at chapter3.simple.RankingTest.savePerson(RankingTest.java:41)
    at chapter3.simple.RankingTest.testSaveRanking(RankingTest.java:72)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    ...
    ...
</code></pre>

<p>What am i doing wrong? thanks</p>

<p><strong>EDIT</strong></p>

<p>Hi, Is the setMaxResults(1)  not suppose to return 1 results from the records?</p>