2012-04-15 92 views
0

工作我的装备:从http://code.google.com/p/hibernate-sqlite/休眠条件不SQLite的

我的实体类是简单

  • 的Hibernate 3.6.9

  • 休眠SQLite的话,

    @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' 
    

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

    我有点为难,试图没有结果一切......

+1

的' '\' '+ UUID + '\'''看起来很奇怪。如果你使用uuid而没有'arround uuid?会发生什么? – andih 2012-04-15 19:44:58

回答

1

你没有我所知,关于Hibernate API(我使用NHibernate的)来设置自己的where子句值远,因此,当您将你的where子句的值设置为'myValue',实际上你正在搜索字符串以匹配'myValue',因为你需要搜索myValue。

更改为这应该希望得到它的工作:

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

这并没有多大帮助,因为SQLite无法处理带连字符的查询,所以我需要引用该参数。没有引号SQLite抱怨: 错误:无法识别的令牌:“4c7e” – 2012-04-15 20:00:11

+0

当您有上述条件查询时,生成的查询是什么? – Baz1nga 2012-04-15 20:30:32

+0

查询与我的文章完全一样,只有在uuid附近没有引号。当我尝试执行此声明时,我得到SQLIte抱怨无法识别的令牌。 我认为这可能是一个SQLite特定的问题... – 2012-04-17 05:56:40