-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Implementaion question, hibernate and struts
PostPosted: Tue Aug 23, 2005 5:21 pm 
Beginner
Beginner

Joined: Tue Aug 23, 2005 4:59 pm
Posts: 31
I am new to hibernate and in building my first project with hibernate I was wondering what the best practice is for using hibernate with struts?

I was going to use the hibernate POJO files as base objects for my Struts ActionForms.. But I relized I have to extend ActionForm and I can just extend a Hibernate POJO file.

So how does everyone do it? How you copy the data from hibernate POJO to ActionForm?

Thanks in Advance!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 5:57 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
commons-beanutils can pass property setter and getter calls through more than one reference.

Code:
class PersonForm extends ActionForm
{
  private Person person;

  Person getPerson()...
  void setPerson(Person)...
}

...
<html:form...>
  <html:text name="person.name"/>
</html:form>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 4:33 am 
Newbie

Joined: Thu Nov 25, 2004 11:44 am
Posts: 15
Location: Woodbridge UK
I'd recommend keeping action forms separate from your Hibernate domain implementation.

Action form properties sometimes need to have String values when your domain model would not. For example, suppose you have a field that contains a number, which the user enters in the text box. If the user enters the format incorrectly, Struts will blank out the value if you represent it as a number, which may not be what you want. For this reason many people use only String values in action form classes.

I'd suggest limiting use of ActionForms to data inputting only. Also, what if you switch to another web framework. Will you still want your Hibernate POJOs to be based on action forms then?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 9:01 am 
Beginner
Beginner

Joined: Tue Jun 28, 2005 4:33 pm
Posts: 21
I agree, keep the actionform and the POJO's separate. I prefer to to use a DynaActionForm so I only need to worry about an xml mapping, rather than a class on the struts side, then just use beanutils to copy to the POJO.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 10:22 am 
Beginner
Beginner

Joined: Tue Aug 23, 2005 4:59 pm
Posts: 31
So how do I use beanutil to copy data from POJOs if the ActionForm doesn't contain even an instance of the POJO?

Does this question make sense?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 10:37 am 
Senior
Senior

Joined: Tue Aug 03, 2004 2:11 pm
Posts: 142
Location: Somerset
ericchile wrote:
So how do I use beanutil to copy data from POJOs if the ActionForm doesn't contain even an instance of the POJO?

Does this question make sense?


We just have a setting of mapping classes that have two abstract methods on them:

mapFormToPojo and mapPojoToForm

In the action of your form, call the mapFormToPojo, which copies (in whatever manner you want), what is in the JSP (form) to the POJO, and similalrly when you call the pre JSP action that populates the form, call the POJO to form mapper.

I wouldn't necessarily use some sort of BeanUtils introspection copy, as your Form may map to fields all over your Pojos and sometimes just doing it manually is easier to understand.

PS have you considered using JSF instead of struts, as JSF handles all this mapping for you ?

_________________
On the information super B road


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 2:37 pm 
Beginner
Beginner

Joined: Tue Aug 23, 2005 4:59 pm
Posts: 31
Faces does huh.... How hard is it to convert from struts to faces?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 3:08 pm 
Beginner
Beginner

Joined: Tue Aug 23, 2005 4:59 pm
Posts: 31
Good article here
http://www.sunnywear.org/AdvantagesOfJa ... ecture.htm


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 4:46 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
ericchile wrote:
So how do I use beanutil to copy data from POJOs if the ActionForm doesn't contain even an instance of the POJO?

Does this question make sense?


BeanUtils.copyProperties(dest,src) will copy all properties that have the same name. So if you a POJO with 4 attributes named id, firstName, lastName, age and an ActionForm with 4 attributes with the same names, executing BeanUtils.copyProperties(form,pojo) will populate all of the attributes from the POJO to the Form.

The nice thing is that your form properties can all be strings or booleans and your POJO properties be typed and it still works.

One thing you might need to do is register default values for some Java types if you allow null values in your database.

Code:
ConvertUtils.register(new SqlTimestampConverter(null), Timestamp.class);
ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
ConvertUtils.register(new LongConverter(null), Long.class);
ConvertUtils.register(new IntegerConverter(null), Integer.class);


Otherwise BeanUtils.copyProperties() throws a ConversionException("No value specified")

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: endless copying, why?
PostPosted: Wed Aug 24, 2005 6:11 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
I would suggest going with nebob's suggestion:
Code:
class PersonForm extends ActionForm
{
  private Person person;

  Person getPerson()...
  void setPerson(Person)...
}

...

and even go one step further and employ Struts nested taglib:

<nested:form...>
<nested:nest property="person">
<nested:text name="name"/>
<nested:text name="alias"/>
.....
</nested:nest>
</nested:form>

Endless copying (even with BeanUtils ) gives nothing but performance degradation.

As for JSF - I do not suggest using it yet.

Cannot go wrong with Tapestry (personal favorite).

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Re: endless copying, why?
PostPosted: Wed Aug 24, 2005 6:43 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
kgignatyev wrote:
I would suggest going with nebob's suggestion:
Code:
class PersonForm extends ActionForm
{
  private Person person;

  Person getPerson()...
  void setPerson(Person)...
}

...

and even go one step further and employ Struts nested taglib:

<nested:form...>
<nested:nest property="person">
<nested:text name="name"/>
<nested:text name="alias"/>
.....
</nested:nest>
</nested:form>

Endless copying (even with BeanUtils ) gives nothing but performance degradation.
...


This will only work if you have all String/boolean values in your POJO or you can gaurtantee that all type-validation is done using JavaScript since ActionForm validation is done AFTER the fields are populated, and if they user enters char values into a Integer field, you'll get Exceptions thrown.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: endless copying, why?
PostPosted: Wed Aug 24, 2005 7:03 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
pksiv wrote:
This will only work if you have all String/boolean values in your POJO or you can gaurtantee that all type-validation is done using JavaScript since ActionForm validation is done AFTER the fields are populated, and if they user enters char values into a Integer field, you'll get Exceptions thrown.


1. If exception will be thrown or not depends on converter implementation;
2. The same exception will be thrown if there is no validation on Action form;

We have few things to consider:
Approach #A:
Configure validation for on every action form - (good idea but really tedious task);
Then somehow define yet another validation of business object on business layer - that is what we need to do if we follow all the good practices, right? So we have couple of places to do the validation.

Approach #B:
Register custom non throwing converters with ConvertUtils;
Have validation defined for that businass object and reuse it;

Approach #C:
some mix of everything + JScript + trust that in 99.5% cases user will not write letters in numeric field.

By the way with Ajax so hot there should not be problem with JScript enabled on clients.....

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Re: Implementaion question, hibernate and struts
PostPosted: Wed Aug 24, 2005 8:00 pm 
Senior
Senior

Joined: Tue Aug 03, 2004 2:11 pm
Posts: 142
Location: Somerset
ericchile wrote:
I am new to hibernate and in building my first project with hibernate I was wondering what the best practice is for using hibernate with struts?

I was going to use the hibernate POJO files as base objects for my Struts ActionForms.. But I relized I have to extend ActionForm and I can just extend a Hibernate POJO file.

So how does everyone do it? How you copy the data from hibernate POJO to ActionForm?

Thanks in Advance!


I think its worthing point out that this thread has now completely digressed from any discussions about Hibernate.

In reality how your domain model is persisted away has nothing to do with the way Struts interacts with your POJOs, in other words your front end code that maps between ActionForms and POJO's should completely unaware of how the POJOs are persisted away, which is a separate tier of your architecture.

Take a step back - just write some code to copies data back and forth between POJOs and ActionForms, and completely divorce any ideas of linkage between this area of your application and the persistence layer. POJOs are after all just that, Plain Old Java Objects, nowt to do with Hibernate, and nowt to do with Struts :-)

_________________
On the information super B road


Top
 Profile  
 
 Post subject: Re: Implementaion question, hibernate and struts
PostPosted: Thu Aug 25, 2005 12:09 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Thanks Paul for pointing out that we've digressed from the Hibernate discussion. As if we didn't realize that talking about BeanUtils and Struts was a different topic.

This is a community forum and someone on this forum, who's new to hibernate, asked a question and a couple of us chose to discuss possible solutions. While the question wasn't specifically about Hibernate, you'll find many discussions on this forum about topics like Spring etc... which also have nothing to do with hibernate but still are asked by hibernate developers and if we choose to answer them, that shouldn't offend you in any way.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: Implementaion question, hibernate and struts
PostPosted: Thu Aug 25, 2005 3:24 am 
Senior
Senior

Joined: Tue Aug 03, 2004 2:11 pm
Posts: 142
Location: Somerset
pksiv wrote:
Thanks Paul for pointing out that we've digressed from the Hibernate discussion. As if we didn't realize that talking about BeanUtils and Struts was a different topic.


There is no need to be sarcastic.

pksiv wrote:
This is a community forum and someone on this forum, who's new to hibernate, asked a question and a couple of us chose to discuss possible solutions. While the question wasn't specifically about Hibernate, you'll find many discussions on this forum about topics like Spring etc... which also have nothing to do with hibernate but still are asked by hibernate developers and if we choose to answer them, that shouldn't offend you in any way.


It didn't offend me at all and I'm as happy as the next guy to help out with struts and JSF or architecture questions. My main point was to clarify that the application should be tiered and to reinforce the point that the original question wasn't in fact related to hibernate. This doesn't mean at all that we shouldn't discuss it, hence me spending a not insigiifcant amount of time answering the question.

_________________
On the information super B road


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.