【问题标题】:Hibernate Criteria does not work in SQLite休眠条件在 SQLite 中不起作用
【发布时间】:2012-04-15 19:35:10
【问题描述】:

我的装备:

我的实体类很简单,

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

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

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

【问题讨论】:

  • '\'' + uuid + '\'' 看起来很奇怪。如果在 uuid 周围没有 ' 使用 uuid,会发生什么?

标签: hibernate sqlite


【解决方案1】:

据我所知,您不必自己设置 where 子句值关于 hibernate api(我使用 Nhibernate),因此当您将 where 子句值设置为“myValue”时,您实际上是在搜索匹配的字符串“ myValue' 在哪里搜索 myValue。

改成这个应该可以让它工作:

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

【讨论】:

  • 这并没有多大帮助,因为 SQLite 不能处理带连字符的查询,所以我需要引用参数。没有引号 SQLite 抱怨:错误:无法识别的令牌:“4c7e”
  • 有上述条件查询时生成的查询是什么?
  • 查询与我的帖子中的完全一样,只是在 uuid 周围没有引号。当我尝试执行此语句时,我得到 SQLIte 抱怨无法识别的令牌。我认为这可能是特定于 SQLite 的问题...
猜你喜欢
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多