Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes building a search feature in my application, what is the best approach Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "building a search feature in my application, what is the best approach" Watch "building a search feature in my application, what is the best approach" New topic

building a search feature in my application, what is the best approach

James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228


I have a Java web application

I have a jsp page that lists all of my objects, say "students" for the sake of arguement

I want to have a form, where I can enter various information such as first name, last name, reference number etc, and to "search" based on the provided criteria, and display a list of results.

From a top level I know I need to do the following

1. Create a form containing all of the search fields
2. On my controller, take in this information and pass it through to the service layer
3. Service layer calls DAO with data and hopefully returns something
4. DAO layer should query to find the objects (this is the bit I'm stuck on)
5. Return all this (or null) back up the stack and redirect to results page

I'm using JPA, with a hibernate implemenation (also using Spring and StripesMVC)

Would it be best, when doing this search, to initialise a "dummy" student domain object, populate the fields from the form, and try to query this?

I'm not asking anyone to do this for me, I would just like some pointers as to where to start. Is there functionality provided by JPA to do what I'm after? Am I on the right lines?

I will now go and read JPA api docs and see what I can find



Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

What's up, J.
Keep in mind that this is just my opinion, ok?

I'd say that depends of the amount of queries you want to perform in an entity. In my opinion, one of the best way to search objects is using NamedQueries. You can type a name for it and a query in JPQL (not very different from SQL). Than, in your DAO, you would create that NamedQuery and get the result.

Something like this

In your DAO, you can have something like this:

Of course, this is a very resumed version of the code. I omitted great part of it. Checkout this doc for more details. In my opinion, it's the best documentation for JPA.

There's other ways to do it. You can create only the JPQL and store it in a class and, when you need it, you can get that JPQL sentence. I don't recommend for you to do only one thing: don't ever write the JPQL code in a class like a DAO. It's very nice to find all the queries in a class (or in the class that refers to that entity or something like that). Another thing you should study is Criteria.

About the 'flow' you said, I guess it's ok. I'd just add a few throws exceptions with the message to present in the screen. Something like... "Oh, we don't have an entity with that name.".

By the way, I'm sorry my english. It's late in Brazil and I'm tired =) Bad day at the office.
I agree. Here's the link:
subject: building a search feature in my application, what is the best approach
It's not a secret anymore!