-->
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.  [ 13 posts ] 
Author Message
 Post subject: Using Hibernate w/ Struts
PostPosted: Mon Mar 08, 2004 9:06 pm 
Newbie

Joined: Mon Mar 08, 2004 8:57 pm
Posts: 6
I'm trying to use Hibernate with Struts and am having a problem. Can I use the bean returned from Hibernate as my Form bean or do I need to create a duplicate Form bean and programmatically move the property values between them?

The problem I


Top
 Profile  
 
 Post subject: Struts Forms are not part of the Model
PostPosted: Tue Mar 09, 2004 12:29 am 
Newbie

Joined: Tue Mar 09, 2004 12:01 am
Posts: 9
Struts forms are not part of the model of MVC, therefore you really should not store them in a DB. You can view them as struts DTO, they help for validating the user input, that's all (cf any book on struts).

That's a pain to write and make a correspondance between your stored objects and struts forms, and that's one of the reasons why I don't really like struts.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 11:19 pm 
Newbie

Joined: Tue Mar 09, 2004 11:17 pm
Posts: 8
You can easily use BeanUtils.copyProperties() to copy properties from your beans to your forms, and vice-versa. It will even convert data types automatically for you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 10:44 am 
Regular
Regular

Joined: Fri Sep 05, 2003 12:01 am
Posts: 80
Location: Bogot
How would this work for you?



Code:
public class MyForm extends ActionForm
{
//Hibernate persisted java object
private MyPOJO myPOJO;

public void setMyPOJO(MyPOJO mp){
myPOJO = mp;
}

public void getMyPOJO(){
return myPOJO;
}

}


In a jsp page when using struts forms:

For this to work your attributes must have setter and getters

Code:
<html:form action="myACTION" name="MyForm">
<html:text property="myPOJO.someAttribute" />
</html:form>


Hope it helps...

_________________
Mauricio Hern


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 11:06 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
and what happens when you call
MyForm.myPOJO.setSomeAttribute() directly on jsp ???
looks dangerous


i use this solution, data tranfer objects are more secure for the presentation layer... it's only my point of view
i'm doing something like Lovecraft
Quote:
You can easily use BeanUtils.copyProperties() to copy properties from your beans to your forms, and vice-versa. It will even convert data types automatically for you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 11:20 am 
Regular
Regular

Joined: Fri Sep 05, 2003 12:01 am
Posts: 80
Location: Bogot
uhm... Im not sure about the dangers you talk about....

Mostly when I use it I rely on validation.

What I like about your approach is that it can handle object types (e.g Dates) easily.

What I dont like is replicating properties in the Action form

_________________
Mauricio Hern


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 11:31 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you can also use DynaForm with copy properties

Have you tried pure jsp code <% persistentObjets.set... %> and look what happens next with the referenced objects on session.flush() for example?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 11:35 am 
Regular
Regular

Joined: Fri Sep 05, 2003 12:01 am
Posts: 80
Location: Bogot
O.K. I get your point and it's valid.

I will have a look at the dynaform. Never used them up to now, the approach I mentioned works well provided you use the html:form tags though, wouldnt you agree?

_________________
Mauricio Hern


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 11:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
try to take a look at DynaValidatorForm, it works great a generate some javascript validation.

I prefer not injecting persistent object in the view...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 10:30 am 
Beginner
Beginner

Joined: Tue Nov 11, 2003 4:49 am
Posts: 47
Location: Florence, Italy
I heavily use Hibernate and Struts in my project and
1) I use directly Model Object if I only have to print their values on the output. All the formatting is done with the formatKey of the bean:write tag and all concern are separated.
2) When I have some inputs I use DynaValidatorForm, and on the Control Layer (aka Struts Actions) I copy the values using copyProperties
I can give you more detail if you are interested.

_________________
Ciao.


Top
 Profile  
 
 Post subject: Don't use Hibernate Java Object for DTO in Struts?
PostPosted: Wed Apr 28, 2004 9:50 pm 
Newbie

Joined: Mon Jan 12, 2004 2:24 am
Posts: 16
delpove writes:
> and what happens when you call
> MyForm.myPOJO.setSomeAttribute() directly on jsp ???

Does it means that Hibernate Persisted Java Object (in the example it's MyPojo is *not* disconnected? If as you say, MyForm.myPOJO.setSomeAttribute() is being called directly on jsp, the values in database's table for that particular object would be updated?

At first I plan to utilize Hibernate Persisted Java Object for DTO (instead of creating another Value Object), but now I have some doubts regarding issue above.

Please help with this.

regards,
Dion

_________________
~Dion


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 3:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If you use the Open Session in View pattern, you can modify and lazy-load objects just like everywhere else in the session scope. I would rather recommend sticking to an MVC architecture though, and just use OSiV only for lazy loading in the view layer.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 8:52 pm 
Newbie

Joined: Mon Jan 12, 2004 2:24 am
Posts: 16
Michael, thanks for the pointer.
right now I'm reading http://forum.hibernate.org/viewtopic.php?t=927886, I must admit that I don't really understand it very well yet :(
BTW, I do want to use it for View only, though.

regards,
dion

_________________
~Dion


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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.