Note that there are some explanatory texts on larger screens.

plurals
  1. POjpa query doesn't seem to resolve enum value correctly and always returns empty list
    text
    copied!<p>I'm writing a very simple query which only queries for Foo objects with a given status, but the result I get is always an empty list, but I really have no idea what was wrong with my code.</p> <p>My <code>findByStatus</code> method looks like this:</p> <pre><code>public List&lt;Foo&gt; findByStatus(final Status status, final int startIndex, final int maxRows) { List&lt;Foo&gt; foos= getJpaTemplate().executeFind(new JpaCallback() { @Override public Object doInJpa(EntityManager entityManager) throws PersistenceException { Query q = entityManager.createQuery("SELECT f FROM Foo f WHERE f.status = :status", Foo.class); q.setParameter("status", status); //This will not automatically resolve??? q.setFirstResult(startIndex).setMaxResults(maxRows); return q.getResultList(); } }); return foos == null ? Collections.EMPTY_LIST : foos; } </code></pre> <p>HOWEVER, if I (1) take out the Foo.class (the second param in <code>entityManager.createQuery</code> and (2) do <code>q.setParameter("status", status.name())</code> instead, then I'll get the correct result list.</p> <p>Does anyone know what's wrong here?</p> <p>By the way, my Foo class that looks like this:</p> <pre><code>public class Foo { @Enumerated(EnumType.STRING) @Column(nullable=false, columnDefinition = "VARCHAR(10) DEFAULT 'ON'") private Status status= Status.ON; //other fields } </code></pre> <p>And status looks like this</p> <pre><code>public enum Status { ON("ON"), OFF("OFF"), UNKNOWN("UNKNOWN"); private String status; private Status(String status) { this.status = status; } public String getStatus() { return status; } } </code></pre> <p>So looks like hibernate bind my enum value to some VARBINARY, which is weird:</p> <pre><code>2013-12-27 11:38:35,341 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - &lt;binding parameter [1] as [VARBINARY] - ON&gt; </code></pre>
 

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