如果我有 20 个或更多的搜索字段并且任何组合都应该是有效的,那么最好的方法是什么。有没有什么特殊的方法可以在 openJPA 或原生 SQL 中做到这一点更好。任何想法都会有所帮助。谢谢。
4 回答
您可以在存储过程或参数化 SQL 语句中尝试类似的操作。通过这种方式,即使只有少数字段具有值,您也可以传入所有字段。
@param1 varchar(25),
@param2 int,
@param3 varchar(10),
@param4 char(1)
SELECT column1, column2, column3, column4
FROM TABLE
WHERE (column1 = @param1 OR @param1 IS NULL)
AND (column2 = @param2 OR @param2 IS NULL)
AND (column3 = @param3 OR @param3 IS NULL)
AND (column4 = @param4 OR @param4 IS NULL)
I think you might want to use something like "Hibernate Search" which brings the power of full text search engines to DB model. Thus, you can provide a google search like feature to do whatever combination of search your customers want to perform.
I had developed a demo app with a GUI to test out various query types.
Check out - http://code.google.com/p/hb-search-demo/
I believe that using criteria api is the best way forward.
Please read that link and check for your own:
I don't like running full text searches through SQL. For stuff like to use a search engine like Solr.
http://lucene.apache.org/solr/
an overview of its features: