[英]jpa query doesn't seem to resolve enum value correctly and always returns empty list
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. 我正在编写一个非常简单的查询,该查询仅查询具有给定状态的Foo对象,但是我得到的结果始终是一个空列表,但是我真的不知道我的代码有什么问题。
My findByStatus
method looks like this: 我的
findByStatus
方法如下所示:
public List<Foo> findByStatus(final Status status, final int startIndex, final int maxRows)
{
List<Foo> 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;
}
HOWEVER, if I (1) take out the Foo.class (the second param in entityManager.createQuery
and (2) do q.setParameter("status", status.name())
instead, then I'll get the correct result list. 不过,如果我(1)取出来让Foo.class(第二PARAM
entityManager.createQuery
和(2)做q.setParameter("status", status.name())
来代替,然后我会得到正确的结果名单。
Does anyone know what's wrong here? 有人知道这是怎么回事吗?
By the way, my Foo class that looks like this: 顺便说一下,我的Foo类如下所示:
public class Foo
{
@Enumerated(EnumType.STRING) @Column(nullable=false, columnDefinition = "VARCHAR(10) DEFAULT 'ON'")
private Status status= Status.ON;
//other fields
}
And status looks like this 状态看起来像这样
public enum Status
{
ON("ON"),
OFF("OFF"),
UNKNOWN("UNKNOWN");
private String status;
private Status(String status)
{
this.status = status;
}
public String getStatus()
{
return status;
}
}
So looks like hibernate bind my enum value to some VARBINARY, which is weird: 所以看起来像休眠将我的枚举值绑定到一些VARBINARY,这很奇怪:
2013-12-27 11:38:35,341 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] - <binding parameter [1] as [VARBINARY] - ON>
I have the same problem in one specific application. 我在一个特定的应用程序中有同样的问题。
The root cause in that hibernate is ignoring the @Enumerated annotion and always passing to the sql the enum as VARBINARY 休眠的根本原因是忽略@Enumerated注释,并将枚举作为VARBINARY传递给sql。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.