Dynamic Queries with Hibernate
I have a client that wants the ability to search on all columns in all tables in their database. So far, I've been able to get all the columns, and their friendly labels by getting all the *Form.* keys in my ApplicationResources.properties file. I still need to sort them alphabetically, but that's another issue. Now I have the following UI pretty much done:
I'm happy with the UI, but I'm struggling with getting Hibernate to return my desired results. I'm able to dynamically select the table/column to search on because of values in my "tables/columns" drop-down. However, many of these tables have Id fields - for status fields, type fields, etc. (basically drop-downs). When searching, this is a pain because users are likely to search on "Active" rather than "1". Because of this, my first issue is how do I make the status field's friendly name a part of my POJO (with Hibernate)?
My 2nd issue is regarding Hibernate's Expression API. I want to be able to pass in the criteria (=, contains, <, >), propertyName and value and get my results. However, it seems that the second half of the comparison must be the same datatype as the field. This means if you're searching on "contract amount", and its a Double, I need to do a Double.valueOf(searchTerm). Basically, I'm looking for an easy way to do this:
Criteria filter = ses.createCriteria(clazz); // determine type of expression - I'd love to figure // out a cleaner way to do this but there's only // 5 possible types, so I don't mind typing the 5 // if statements if (expression.equals("=")) { // How do I convert these to the fields' type at run-time? // Sure I could do a bunch of if statements again, but it seems ugly. filter.add(Expression.eq(field, Double.valueOf(value))); } List results = filter.list();
Any advice is appreciated - especially considering I estimated this task to take 4-5 hours and I'm going on 8.
Posted by Ryan Campbell on October 05, 2003 at 10:41 PM MDT #
Posted by Damien on October 05, 2003 at 10:55 PM MDT #
Posted by Damien on October 06, 2003 at 12:53 AM MDT #
Posted by Vic Cekvenich on October 06, 2003 at 01:47 AM MDT #
Posted by Matt Raible on October 06, 2003 at 02:26 AM MDT #
Now, using a diferent "connection" populate the options collection. It "joins" on the browser, what the users sees. .V
Posted by Unknown on October 06, 2003 at 03:07 PM MDT #
So my question is - <strong>How do I search on the text value of a code property <em>with Hibernate.</em></strong>
Posted by Matt Raible on October 06, 2003 at 03:46 PM MDT #
Posted by Bear Giles on October 06, 2003 at 05:33 PM MDT #
Posted by Scott Mitchell on October 07, 2003 at 01:02 AM MDT #
Posted by just a suggestion on October 08, 2003 at 07:22 AM MDT #
Posted by 200.75.13.148 on March 29, 2006 at 08:29 PM MST #