简体   繁体   English

Hibernate:如何创建已保存的过滤器然后使用它们?

[英]Hibernate: How can I create saved filters and then use them?

This is the problem: 这就是问题:

I receive a task for a project which uses Spring, Hibernate and Wicket. 我收到一个使用Spring,Hibernate和Wicket的项目的任务。

In a particular HTML page I must have the possibility to create a filter (set the name of the filter and its parameters). 在特定的HTML页面中,我必须有可能创建一个过滤器(设置过滤器的名称及其参数)。 I must create a list of filters in this way. 我必须以这种方式创建过滤器列表。 In the same time, I must have the possibility to edit and delete the filter, and of course, to use that filter. 同时,我必须有可能编辑和删除过滤器,当然,也可以使用该过滤器。

Any ideas? 有任何想法吗? How could I do this? 我怎么能这样做?

My experience with Hibernate is very low, but I need to come with an idea for a project. 我对Hibernate的体验非常低,但我需要为项目提出一个想法。

My naive solution: 我天真的解决方案:

I have the class Table_Something, to which I have associated a table in my database with Hibernate, and I need to create the possibility for the user to create filters for this table in my web application 我有类Table_Something,我已经将数据库中的表与Hibernate关联,我需要创建用户在我的Web应用程序中为此表创建过滤器的可能性

So, I would create a class, named Table_Something_Filters, to which I will associate also a table in my database with Hibernate. 所以,我将创建一个名为Table_Something_Filters的类,我将把我的数据库中的表与Hibernate关联起来。 So when, I create a new filter for Table_Something in my application, I would insert a new row in my Table_Something_Filters. 所以当我在我的应用程序中为Table_Something创建一个新的过滤器时,我会在Table_Something_Filters中插入一个新行。

Is this a good idea? 这是一个好主意吗? Any improvements for my solution? 我的解决方案的任何改进?

Another problem: How can I use my filter? 另一个问题:我如何使用我的过滤器? I query the Table_Something_Filters to get the values for the parameters of the filter and then what? 我查询Table_Something_Filters来获取过滤器参数的值然后是什么? How can I generate the finder, or how could I query the Table_Something, based on the values from the Table_Something_Filters? 如何根据Table_Something_Filters中的值生成查找程序,或者如何查询Table_Something?

Any ideas or suggestion are welcomed! 欢迎任何想法或建议! :) :)

Well, yes, you could save in your *_FILTER table the parameters and values the user entered as a filter and then in your DAO have a method like this: 好吧,是的,您可以在* _FILTER表中保存用户输入的参数和值作为过滤器,然后在您的DAO中有一个这样的方法:

public List<MyEntity> findByFilter(MyEntityFilter filter) {
   //Use hibernate criteria api to build up your query depending on value in your filter
   Criteria criteria = session.createCriteria(MyEntity.class)

   if (filter.getName() != null) {
      criteria.add( Restrictions.like("name", filter.getName())
   }
   ... Go on testing and adding other parts of the query

   return criteria.list();

The MyEntityFilter class would be a simple class with getters and setters for each parameter of your possible search filter. MyEntityFilter类将是一个简单的类,其中包含可能的搜索过滤器的每个参数的getter和setter。 This Filter class you would have build it up with the values you stored in your database. 您将使用存储在数据库中的值构建此Filter类。 If filters don't often change you can cache them in your application. 如果过滤器不经常更改,您可以将它们缓存在应用程序中。 If you have only a few filterable parameters you can store them in separate columns, else you would rather store a string like this: name=John ;color=blue,red,yellow;parameter3=value* and then parse the string to fill up your Filter objects. 如果你只有几个可过滤的参数,你可以将它们存储在不同的列中,否则你宁愿存储这样的字符串: name = John ; color = blue,red,yellow; parameter3 = value *然后解析字符串以填充你的过滤器对象。 Be careful about your separator chars you use, you should escape them from your user input! 小心你使用的分隔符字符,你应该从用户输入中删除它们!

Then, you can dynamically build up a search criteria with only the filled in fields of your Filter class. 然后,您可以仅使用Filter类的填充字段动态构建搜索条件。

If you are using spring with hibernate have a look at their HibernateDaoSupport , and you could also search for the specification pattern which is often used for this complex query building. 如果你使用带有hibernate的spring看看他们的HibernateDaoSupport ,你也可以搜索通常用于这个复杂查询构建的规范模式。

Off course you don't have to use the criteria api, you could build up a HQL query string also, but the criteria api is more appropriate for this kind of stuff (not always more easy to read though). 当然你不必使用标准api,你也可以建立一个HQL查询字符串,但是标准api更适合这种东西(虽然并不总是更容易阅读)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM