-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Best practice for hibernate when use with JSP
PostPosted: Thu May 11, 2006 7:13 am 
Regular
Regular

Joined: Wed Apr 12, 2006 12:49 am
Posts: 105
Location: Malaysia
Hi,

I'm using Hibernate 3.1.3 and using JSP for development.

I read from some hibernate examples that we should put persistence related coding in DAO classes.

I'm designing a search page where user can enter one or multiple search criteria values. For example, user can search by title, or author, or subject or isbn or combination of these.

Let say I have a Book class (the POJO) and a BookDAO class. I know my BookDAO will need a method call searchBook. My doubt starts here.

I need some coding that dynamically determine what search criteria was entered by user then construct the required conditions for selecting from the class. Should I put this coding inside the BookDAO's searchBook method or I just code it in the JSP ?

If put the code in BookDAO, that means I have to fix the method to accept various parameters. But these parameters might be different based on the search criteria that the user has entered. Most of the example and reference book I read have their method accept specific parameters only, which I think I can apply in my case.

If put the code in JSP, then we will have hibernate persistence coding inside JSP? Do most of you put hibernate related coding in JSP as well? Another concern is i afraid these code in JSP will be expose to others especially if we are building a product and deploy at a customer side.

So, should I put in JSP or DAO. If put in DAO, how we usually make the method that select from class to accept dynamic condition, different order by and group by clause.

Please advise.
Thank you.


Top
 Profile  
 
 Post subject: MVC Design pattern
PostPosted: Sun May 28, 2006 10:50 am 
Newbie

Joined: Mon Jun 20, 2005 2:51 pm
Posts: 13
Location: Brasil
Hi,

as a rule of thumb you should never put code into jsp pages, this is a maintenance nightmare in the long run. You should apply the MVC (model-view-controller) design pattern. Use Jsp pages only to render the informations, put all the java code in a separate class which constructs DAO objects and there issue the database queries. The results are then bound to the request, or session object (request.setAttribure("results", resultlist)) and can be accessed by the jsp.

Hope this helps,

Axel

_________________
Skype: 'axelberle'


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 28, 2006 6:14 pm 
Beginner
Beginner

Joined: Fri Mar 17, 2006 7:30 pm
Posts: 32
Using MVC, you will need a Model object. That will in turn call the DAO. If the number of parameters might vary and there might be a possibility of adding or subtracting choices, DAO take a single class as a parameter called something like SearchParametersBean. The model will make one of these and fill it depending on options supplied by the user. This gets passed to the DAO.

You should also consider a Controller framework such as Struts or Spring MVC to controlling the JSP. Spring also has a lightweight framework to handle the creation of the Model and the DAO.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 29, 2006 2:09 am 
Newbie

Joined: Mon May 29, 2006 12:57 am
Posts: 11
Location: india
NEVER EVER PUT CODE IN YOUR JSP. Very bad coding practice.

You can always capture as to what is the search criteria that the user has entered in the screen. You can use Struts to control ur JSPs or pass it on as sesion objects, capture them in the underlying layer and call the respective queries in the DAO layer.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 29, 2006 2:10 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Hi,

Just to add. Our other posters are right about going to MVC.

For dynamic search, you should use the Criteria of Hibernate. This will make your job simple for the search condition and parameters.

Criteria will construt the Query on the runtime based on the values you have entered in the JSP. Read the below link to know more about Criteria.

http://www.hibernate.org/hib_docs/v3/re ... rycriteria

All the Best

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.