Hibernate如何将Criteria转换为HQL?

发布于 2024-12-08 19:02:43 字数 1086 浏览 0 评论 0原文

我的条件查询有问题。

在另一种方法中,我使用 HQL 查询来删除数据库中的某些对象。 查询有效。

实际上,在另一种方法中,我执行获取对象的条件查询。 当我获取对象时,它们没有同步到数据库。

所以 1.如何在roder中同步此条件查询以获取REAL对象? 2.如果1.不可能,我想将Criteria查询转换为HQL

这里是我的条件查询:

    final Criteria crit = session.createCriteria(ObjectDao.class);
    if (clientName != null && clientName.length() > 0) {
        crit.createAlias("objectType.client", "client");
        crit.add(Restrictions.eq("client.name", clientName));
    }
    if (objectType != null && objectType.length() > 0) {
        crit.createAlias("objectType", "objectType");
        crit.add(Restrictions.eq("objectType.type", objectType));
    }
    final List<ObjectDao> ret = crit.list();

并且HQl转换后的查询不起作用

    String hqlQuery = "select ObjectDao where objectType.client.name = :clientName";
    Query query = session.createQuery(hqlQuery)
    // .setParameter("objectList", objectType)
            .setParameter("clientName", clientName);
    final List<ObjectDao> ret2 = query.list();

谢谢!

I have a problem with a Criteria query.

In an other method I use HQL query in order to remove some objects in database.
The query works.

In another method I do, actually, Criteria query which get objects.
When i get objects , they are not synchronized to the database.

So
1. How synchronized this criteria query in roder to get REAL objects?
2. If 1. is not possible, i want to transform Criteria query to HQL

Here my criteria query:

    final Criteria crit = session.createCriteria(ObjectDao.class);
    if (clientName != null && clientName.length() > 0) {
        crit.createAlias("objectType.client", "client");
        crit.add(Restrictions.eq("client.name", clientName));
    }
    if (objectType != null && objectType.length() > 0) {
        crit.createAlias("objectType", "objectType");
        crit.add(Restrictions.eq("objectType.type", objectType));
    }
    final List<ObjectDao> ret = crit.list();

And HQl transformed query which dont work

    String hqlQuery = "select ObjectDao where objectType.client.name = :clientName";
    Query query = session.createQuery(hqlQuery)
    // .setParameter("objectList", objectType)
            .setParameter("clientName", clientName);
    final List<ObjectDao> ret2 = query.list();

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦与时光遇 2024-12-15 19:02:43

看起来您正在使用 2 级缓存。如果是这样,这也行不通。

select o from ObjectDao o where o.objectType.client.name = :clientName

Looks like you are using Level 2 cache. If so this will not work either.

select o from ObjectDao o where o.objectType.client.name = :clientName
忘东忘西忘不掉你 2024-12-15 19:02:43

如果我理解正确的话,您正在执行一个查询,例如

delete from Client where ...

,然后执行一个 Criteria 查询,该查询返回 ObjectDaos 仍然具有您刚刚删除的客户端。

如果这是正确的,那么这是预期的行为。 DML 样式查询(即更新和删除查询)绕过会话。这意味着,如果您删除的某些对象在删除查询之前已经加载到会话中,Hibernate 不会从会话中删除它们,并且会话不会反映数据库的实际状态。

使用 Session.delete 删除对象,并确保在内存中维护对象图。

或者在删除查询后刷新并清除会话。

If I understand correctly, you're executing a query such as

delete from Client where ...

and then execute a Criteria query which returns ObjectDaos still having a client that you just deleted.

If that's right, then it's expected behavior. DML-style queries (i.e. update and delete queries) bypass the session. This means that if some object that you deleted was already loaded in the session before the delete query, Hibernate won't delete them from the session, and the session won't reflect the actual state of the database.

Use Session.delete to delete your objects, and make sure to maintain your object graph in memory.

Or flush and clear the session after the delete query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文