Is there any easy way to convert Criteria to HQL?

狂风中的少年 提交于 2019-11-30 16:41:16

问题


I have posted a question few days ago about Querying on collections with the Criteria API and after all the answers I see that the thing that I am trying is not possible with the Criteria, there is a bug for the situation in nhibernate and also in hibernate

I was using DetachedCriteria to get all criterias together and the list is really long. The last circle of chain is buggy criteria so I need to change all my detachedcriteria to HQL.

So my question is is there any tool or way to convert Criteria to HQL.

or

Is there any way to get Criteria and Hql work together?

An Article about the bug in nhibernate


回答1:


reposting from the hibernate forums... not sure if it is relevant to nhibernate or up to date


from: max Hibernate Team

Posted: Sun Dec 24, 2006 7:12 am

it is a very stubborn folklore that criteria translates to HQL. Criteria goes directly to SQL, but there is no direct public api to get it programmatically.

...

translating twice isn't very efficient ;)

http://forum.hibernate.org/viewtopic.php?t=968990&view=next&sid=50454aa6cec8d482198117fbd398ca70




回答2:


I have sent an email to Ayende Rahien about the issue and thank to him for replying.He said that

the answer is that there is no such tool, but conversion is usually pretty easy.




回答3:


I have modified the Hibernate Criteria API to generate JPQL queries by trying to preserve the interface mostly similar. Name of my project is Criteria2JPQL at:

https://sourceforge.net/projects/criteria2jpql/

Since this translation is a simple in-memory operation that generates a standard JPQL query, there is not a considerable performance problem. On the other hand, the idea of this project has originated from the poor performing native sql queries of Hibernate Critera including lots of unnecessary joins. We have experienced using Criteria2JPQL in our project at my company and observed those unnecessary joins disappeared.




回答4:


What version of nHibernate are you using? I think using the latest version might solve your problem as the 2.0 GA has a lot of breaking changes.



来源:https://stackoverflow.com/questions/543656/is-there-any-easy-way-to-convert-criteria-to-hql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!