• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Hibernate error when loading JSP

 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if this is a Hibernate problem or a JSP issue, but I'm leaning towards JSP as everything else works correctly.

I save the user object in my session. So at the top of my page I have the following.



This works on my other pages to allow me to get the various properties of the user bean.

However, I also have another object named agency. Each user has a reference to an agency. Now I need to access the agency properties. So underneath the above line of code, I have the following.



Whenever I load this page, I get the following Hibernate error.



I'm assuming that since agency data is stored in another table, it's doing a hibernate query vs the normal get/set of the regular properties.

I'm at a loss. Anybody have any suggestions?

TIA.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either use an OSiV filter (meh--can cause performance issues if you're not careful), or initialize what you're looking at in the backing servlet/action/controller/whatever.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Either use an OSiV filter (meh--can cause performance issues if you're not careful), or initialize what you're looking at in the backing servlet/action/controller/whatever.



I'm not sure what you mean. How can I initialize Hibernate in a JSP? Do I just add the transaction code in the <% %> block?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose you *could*, but that kind of code really doesn't belong in a JSP.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:I suppose you *could*, but that kind of code really doesn't belong in a JSP.



That's what I thought. And why I'm not sure how to initialize this.

I have another page where I make the same call and it works fine. However, I only check if it's null or not. I never actually call any of the properties of agency.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pretty much all your Hibernate stuff should be handled on the back-end. Are you using the "open session in view" filter? (PS. don;t be confusing Hibernate sessions with servlet sessions -- not related).

Are you placing a User instance into the scope before the JSP is loaded?

(And, of course, at this stage of the game, scriptlets should not be used in JSP pages.)
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Pretty much all your Hibernate stuff should be handled on the back-end.



That's what I'm doing now. Each servlet gets the current hibernate session and performs the needed transactions.

Are you using the "open session in view" filter?



Um, no? So far I've just been doing it inside each servlet.

(PS. don;t be confusing Hibernate sessions with servlet sessions -- not related).



I realized that.

Are you placing a User instance into the scope before the JSP is loaded?



Yes. The user stuff works fine everywhere else. It's just when I try and access the agency properties that I get this error.

(And, of course, at this stage of the game, scriptlets should not be used in JSP pages.)



Are <% %> the same thing as the scriptlet tag? What would you recommend instead?
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scriptlets are outdated by about 6 years. Modern JSP uses JSTL and EL.

But back to the issue... the error you are getting either means that your object is not known to the Hibernate session (in other words, it's a transient or detached object), or the session is not open.

At this point, I'm going to declare that this is a Hibernate issue and shuffle it along to the ORM forum where the more Hibernate-savvy than myself (not hard) can pontificate.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anybody have any Hibernate suggestions?
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check to make sure that the object is participating in a Hibernate session before forwarding to the JSP page, and make sure that the Hibernate session is active during the JSP processing.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do a Hibernate.initialize(object.getWhateverCollectionIsMissing()) in the servlet, inside the Hibernate session/transaction.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm changing my code to implement Hibernate's Open Session in View.

https://www.hibernate.org/43.html

However, I'm not sure how I can communicate the success or failure of the transaction. In my current code, I call a commit, and then if it works, I direct the users to one page. If the commit fails, I rollback and direct the users to another page.

Since I'm using a filter, how do I determine whether the commit worked or not?
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're micro-managing your error handling.

You should have a centralized error handler. Don't bother checking in the servlets. If something fails, an exception is thrown and will be handled in one place.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You're micro-managing your error handling.

You should have a centralized error handler. Don't bother checking in the servlets. If something fails, an exception is thrown and will be handled in one place.



And the way to do that would be to set up a Filter to catch any errors?

The other problem I'm having right now is how to properly load my objects in the JSP. Since I'm now storing just the user id, I need to have some way to convert that to an actual user object. I'm looking through the jstl stuff now, but I'm not sure what the proper way is.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All Hibernate/DB stuff should be handled in the servlet/whatever, not in the JSP. Don't store the user ID in the session, otherwise you have to look up the user on every request: store the user object in session.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:All Hibernate/DB stuff should be handled in the servlet/whatever, not in the JSP. Don't store the user ID in the session, otherwise you have to look up the user on every request: store the user object in session.



I was storing the user object in the session, but that's when I was getting the no session errors. I'm not really sure how to avoid having hibernate information in the JSP.

I've looked around and so far haven't been able to find any hibernate/jsp examples. If you know of any or anyplace listing best practices for integrating the two, I'd appreciate it.

And I'm not sure where I saw the setup to use the id instead of the actual user. I think it was the DAO setup in the Open View article I linked above. And even if I store the user in the session variable, I still need to make the agency call which invokes hibernate.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I already told you: initialize the collection in the servlet, not the JSP.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bai Shen wrote:And the way to do that would be to set up a Filter to catch any errors?

No, use the error handling capabilities of the deployment descriptor.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:I already told you: initialize the collection in the servlet, not the JSP.



Okay. How do I do that?

Sorry for being obtuse, but unfortunately this web stuff isn't clicking for me. :(
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has nothing to do with web stuff though--this is a Hibernate issue ;)

Call Hibernate.initialize(theUser.getAgency()).

Or just don't make it lazy, if you always need it.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

Bai Shen wrote:And the way to do that would be to set up a Filter to catch any errors?

No, use the error handling capabilities of the deployment descriptor.



Ah, okay. Now I have a different problem though.

The server can't seem to find my error page. I set the location to "/error.html" but it won't find it. Is there a way to designate the root of an app?
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:This has nothing to do with web stuff though--this is a Hibernate issue ;)

Call Hibernate.initialize(theUser.getAgency()).

Or just don't make it lazy, if you always need it.



Okay, but where do I put that? I only need the agency info for one page, and that page is a JSP. It's just a form where I display the properties of the agency. Or are you saying that as soon as the user logs in, that I need to do the initialization?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm saying it should go into the servlet that's used to display the JSP, or if it's not a huge object just initialize it on login and it'll follow the user around in the session.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:I'm saying it should go into the servlet that's used to display the JSP, or if it's not a huge object just initialize it on login and it'll follow the user around in the session.



Isn't the whole point of JSP's to not have a servlet where you have to maintain the html in the code?

I guess that's why I'm confused. I don't have a servlet that's displaying the JSP. I have a page that just links to the JSP.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point of the servlet/JSP combination is to move Java code into the servlet, and to keep the JSP as simple as possible. HTML generation should be done in the JSP. Java code should be done in the servlet.
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:The point of the servlet/JSP combination is to move Java code into the servlet, and to keep the JSP as simple as possible. HTML generation should be done in the JSP. Java code should be done in the servlet.



Is this what y'all have been suggesting?

Send the html request to a servlet which loads/massages my data, then redirect from the servlet to a jsp page which displays the data?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep :)
 
Bai Shen
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Yep :)



Interesting and totally not what I have currently. :)

I'll have to take a look at refactoring what I have. Right now I just added initialization code to my login page.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The issue is that you are using fetch type=lazy in your mapping file. If fetch type is defined as lazy then you cannot load the data after hibernate session has been closed. There are two ways to solve this issue.

1. Make sure your hiberante session is open means that you need to fetch user object in JSP itself not putting in session object.
2. Otherwise, before closing your hibernate session, make sure to get this value (theUser.getAgency) . Means just access this value or print this value. whenever you will try to access this value, hibenate will load the data. After that you can close hibernate session.
 
I brought this back from the farm where they grow the tiny ads:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic