Hibernate如何将Criteria转换为HQL?
我的条件查询有问题。
在另一种方法中,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在使用 2 级缓存。如果是这样,这也行不通。
Looks like you are using Level 2 cache. If so this will not work either.
如果我理解正确的话,您正在执行一个查询,例如
,然后执行一个 Criteria 查询,该查询返回 ObjectDaos 仍然具有您刚刚删除的客户端。
如果这是正确的,那么这是预期的行为。 DML 样式查询(即更新和删除查询)绕过会话。这意味着,如果您删除的某些对象在删除查询之前已经加载到会话中,Hibernate 不会从会话中删除它们,并且会话不会反映数据库的实际状态。
使用 Session.delete 删除对象,并确保在内存中维护对象图。
或者在删除查询后刷新并清除会话。
If I understand correctly, you're executing a query such as
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.