This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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
Author

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

James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

Hi,

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

Thanks

J


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: http://aspose.com/file-tools
 
subject: building a search feature in my application, what is the best approach
 
Similar Threads
Injecting a session bean into an application bean
Need your opinions on this !
Design decisions - Choose the layers and their role
Implement Transactions best way, in my application
passed SCJD with 360/400