NHibernate 用户定义的查询
我需要实现一个查询表单,让用户有机会 建立自己的标准, 基于选择属性,运算符(=、<>、like、not like、in、 不在...)和一个值, 将它们与 AND 、 OR 逻辑运算符结合起来。 我想存储 有单独的标准 我的数据库中的实体。
以前有人有过这样的要求吗? 我应该走哪条路:ICriteria 还是 HQL? 还有其他选择吗?
我非常感谢任何想法、建议、指示……
万分感谢。 达科
I need to implement a query form giving the user the opportunity to
build his own criterias,
based on selecting a property, an operator (=, <>, like, not like, in,
not in ...) and a value,
combining those with AND , OR logical operators. I wanted to store
there criterias in a separate
entity in my db.
Has anyone had this kind of a requirement before?
Which path should I go: ICriteria or HQL? Is there another option?
I am very thankfull for any ideas, suggestions, pointers...
THX a mil.
Darko
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我实现了一个非常相似的要求。 我使用 Criteria API,因为您不必使用字符串操作将查询放在一起,这使得它更加稳定。
我在这个问题中发布了一个简单的示例,如何将动态查询放在一起。
在我们的解决方案中,我不允许 OR 运算,因为它使它变得非常复杂(也是 UI 部分)并且性能可能会变差。 但后来,我们可能也会实现它。
需要根据您的要求以及查询和数据结构的复杂性做出设计决策。
例如,我为每个查询创建了一个过滤器类。 过滤器类包含预定义的字段。 这使其更加稳定,因为您始终知道哪些字段可能存在,并且可以将它们放在查询中的某些位置。 有些字段需要子查询。 在我们的例子中,不可能使其完全通用(这意味着:应如何构建查询的所有信息都存储在过滤器中)。 每个过滤器类都有特定的方法将其转换为查询。 这给了你很大的灵活性。
我的标准包括对字段的引用、运算符(枚举)和参数。
I implemented quite a similar requirement. I'm using Criteria API, because you don't have to use string operations to put you query together, which makes it more stable.
I posted a simple example in this question, how to put a dynamic query together.
In our solution, I didn't allow OR operations, because it makes it very complicated (also UI part) and performance could get bad. But later, we probably implement it as well.
Design decisions need to be made according to your requirements and the complexity of your queries and data structure.
For instance, I made a filter class for each query. The filter class holds predefined fields. This makes it more stable, because you always know which fields could be there, and can put them on certain places into the query. Some fields require subqueries. In our case it is impossible to make it fully generic (this means: that all the information how the query should be built up is stored in the filter). There is specific method for each filter class to turn it into a query. This gives you a lot of flexibility.
My criteria consist of a reference to a field, an operator (enum) and an argument.