• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using Hibernate in a web application

 
Francesco Marchioni
author
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
I'm starting my first project using Hibernate.
I have to use it in a Web application that uses Struts as Framework.
I'd like to have some advice about how to pass DB Data to JSP for displaying rows.
In other words, should I pass the "live" Hibernate Object (that maps the DB column) directly to the JSP. (With the <jsp:useBean> for example) and the JSP browses the object.
Or should I create a ValueObject (a copy of the Hibernate Object) and pass it to the JSP ?
Which is the best practice ?
Thanks
Francesco
 
sathish kumar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U might want to keep your hibernate objects out of the form objects of struts.
It is always a good practice to keep your UI layer away from the business layer, It will give you more control when a client requirement changes or business requirement changes.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"satish ranch user"

Welcome to JavaRanch!

Can you please update your display name so it complies with our Naming Policy. You can do this here.

Thanks!
 
Jeroen Oosterlaar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate 'table representation' beans are just plain JavaBeans, so I cannot think of a reason not to use them for presentation purposes.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Francesco Marchioni:
Hi all!
I'm starting my first project using Hibernate.
I have to use it in a Web application that uses Struts as Framework.
I'd like to have some advice about how to pass DB Data to JSP for displaying rows.
In other words, should I pass the "live" Hibernate Object (that maps the DB column) directly to the JSP. (With the <jsp:useBean> for example) and the JSP browses the object.
Or should I create a ValueObject (a copy of the Hibernate Object) and pass it to the JSP ?
Which is the best practice ?
Thanks
Francesco


Will these objects be serialized? Is it just for display or any modification?
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A major problem with ORM frameworks trying to pass objects out to the display layer is that the persistable objects often carry some extra freight related to the persistency mechanism - specifically, JDBC connection objects. This information generally gets ripped loose by the time the display layer gets it, so indirect refences like <c ut value="${object1.object2.property1}"/> end up failing or showing as null objects.

To function properly, you have to ensure that all the dependent items are force-fetched. That way, they can be located without reference to the persistency subsystem. Since lazy fetching is the norm, you may have to "nudge" the object structure to get everything resolved in the pre-display code while the persistency infrastructure is still active.

JDO can be a real pain in that regard. Hibernate often handles things more gracefully, but even it needs some manual help at times.

Just in case it wasn't clear, the answer is "yes. IF...".
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use the Session In View pattern to have the Session available to the jsp page so that it can lazy load and load at time of view creation.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic