-->
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: Newbie question: Form bean or normal bean
PostPosted: Wed Jan 11, 2006 11:23 am 
Newbie

Joined: Wed Jan 11, 2006 11:09 am
Posts: 2
I am very new to Hibernate and was shown a few things on how to use it with Struts and mapping to form beans.

My question is whether it's not better to use a plain java bean which mirrors the table layout instead of a form bean which could have a completely different set of fields?

I was told to use form beans but it doesn't make sense to me.

Thanks for any replies.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 11:30 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Recommend you seperate your presentation from Data layer. So do the Database mapping using POJOs and then create your form beans and copy properties across.
If you work backend out, you could create your POJO's and then just add struts xdoclet to create the form beans. Then in the Struts actions use a BeanUtils.copyProperties(). However, quite often you end up changing the form bean to handle comma seperated stings for example, which will translate to a set of objects in the DB layer.

So in short, you can do it using the same objects but often it's not good enough and has too much coupling.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 11:31 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
And if the form has a different set of fields then you definitely need to separate them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 11:47 am 
Newbie

Joined: Wed Jan 11, 2006 11:09 am
Posts: 2
Tim,

What I wanted to do was to still use form beans to receive the posted info but then move the fields to a hibernate mapped pojo if I need to insert a record in the db. Is this what you mean?

Right now we are mapping the form bean to hibernate and I can't understand why.

Thanks for the reply. I am desperately trying to do this the right way from the start so any help is greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 4:45 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
That is exactly what I mean.

Let's say you are doing a create user

    - The form is posted with the user details (UserForm) and validated etc. The form may be something like a foo.bar.web.form.UserForm.
    - The action would then call some business method on your service layer called say UserManager.createNewUser(User user).The User is the object that is mapped to the database and could well be something like foo.bar.model.User.
    - If they are very similar you can make use of BeanUtils.copyProperties(userForm, user) from the common beanutils package. This will copy all the properties across that are the same name.


What you are trying to do here is offer some seperation between the web layer (E.g. the user interface) and the business services that you offer. Remember in many systems you have more than one access to your business layer - it may be that you offer SOAP services through the same UserManager object as well as a struts web application.

It's termed (here) a 3 tier architecture. Data Access (Where you do your Hibernate load, query etc), Business Logic (A nice interface to the hibernate logic with UserManager.createUser() type interfaces) and Presentation (Struts which will issue calls to the Business Layer).

I think you have the right idea though or else you wouldn't be querying WTF they are mapping struts forms to the DB...

P.s. If it is a really really simple application (and I mean really simple), then maybe mapping forms to the DB is acceptable, but not exactly good for maintainability, extensibility etc etc...

p.p.s. - my comment about generating the struts forms - you can use xdoclet tags in the mapped pojo and then generate a form from the pojo. This is not bad practice to as it saves you work - but normally I find you can do this once and then you change the form so can't do it any more.


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.