%--
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
--%>
<%@page import="openbook.server.OpenBookService"%>
<%@page import="openbook.domain.Book"%>
<%@page import="javax.servlet.http.HttpServletRequest"%>
<%@include file="header.jsp"%>
Criteria Query & Form-based Search
This is a typical search form in a web page. The user fills in one or more fields
about a Book, clicks
Search and a set of Books matching
the user criteria appear on the web page.
- Dynamic Query:
Behind the page, the user input will be used to build up a query, executed on a database and the results
returned. The problem is how to build the right query based on the fields that the user had filled in.
If there are 6 input fields -- potentially there are
26=64
ways to fill in the form and, hence, 64 possible queries.
Criteria Query -- introduced in JPA 2.0 -- can solve this combinatorial problem
by building the query
dynamically.
The code shows how the predicates are created based on availability of particular input fields.
In the end, all the predicates are anded together to create the final selection criteria.
- Safety by Strong Typing: This new query API is also strongly typed via usage of generic
type arguments.
For example, the API signature enforces that the type of the result returned by a query must match
the type of arguments selected. Or, a String field can not be compared by mistake against a numeric
value. All these new features reduces the risk of runtime errors that can be caused by String-based
query.
More about Criteria Query can be found
here.
<%!
public static String getParameter(HttpServletRequest request, String param) {
return getParameter(request, param, true);
}
public static String getParameter(HttpServletRequest request, String param, boolean replaceNull) {
String value = request.getParameter(param);
return replaceNull ? (value == null ? "" : value) : value;
}
%>
<%
OpenBookService service = (OpenBookService)session.getAttribute(KEY_SERVICE);
if (service == null) {
%>
<%
}
%>
Fill in the details for a book you are searching for.
Search Tips:
- You can leave one, more or all fields empty.
- OpenBooks database currently contains <%= service.count(Book.class) %> books.
- Book titles are
Book-1
, Book-2
, Book-3
,...
- Author names are
Author-1
, Author-2
, Author-3
,...
- Both Book and Author names accept wildcard characters.
For example, an underscore like Book-3_
will match any single character to return
Book-31
, Book-32
, Book-33
...
<%@include file="footer.jsp"%>