0

我的装备:

我的实体类很简单,

@Entity
@Table(name = "RegistrationRequests")
public class RegistrationRequest {

    @Id
    @GeneratedValue
    Integer id;
    String uuid;
    ... getters and setters ...
}

uuid 被分配给 UUID.randomUUID().toString(),并正确地保存在数据库中。现在,问题是当我尝试使用 Hibernate Criteria 从数据库中检索存储的请求时。

   Criteria criteria = sessionFactory.getCurrentSession().createCriteria(RegistrationRequest.class);
    criteria.add(Restrictions.eq("uuid", '\'' + uuid + '\''));
    List requests = criteria.list();

总是产生一个空的结果,但是如果我执行生成的 SQL,

select
        this_.id as id2_0_,
        this_.created as created2_0_,
        this_.email as email2_0_,
        this_.isVerified as isVerified2_0_,
        this_.name as name2_0_,
        this_.password as password2_0_,
        this_.surname as surname2_0_,
        this_.uuid as uuid2_0_ 
    from
        RegistrationRequests this_ 
    where
        this_.uuid='ced61d13-f5a7-4c7e-a047-dd6f066d8e67'

结果是正确的(即从数据库中选择了一条记录)。

我有点难过,尝试了一切都没有结果......

4

1 回答 1

1

you dont have to set the where clause value yourself as far as I am aware about hibernate api (I use Nhibernate), thus when you set your where clause value as 'myValue', you are actually searching for string to match 'myValue' where as you want o search for myValue.

Changing to this should hopefully get it working:

Criteria criteria = sessionFactory.getCurrentSession().createCriteria(RegistrationRequest.class);
    criteria.add(Restrictions.eq("uuid", uuid));
    List requests = criteria.list();
于 2012-04-15T19:52:16.047 回答