Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7795609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:07:06+00:00 2026-06-01T23:07:06+00:00

I save current user in session, and when i use current user (example:user.getRole.getRoleName()), i

  • 0

I save current user in session, and when i use current user (example:user.getRole.getRoleName()), i got LIE. How can i solve this problem, my code is like this

Controller:

public String home(){
    Users users = userService.getCurrentUser();
    if(users.getRole().getRoleName().equals("admin")){ //causing LIE
    ....
}

UserService :

@Override
public Users getCurrentUser(){
    session =  ActionContext.getContext().getSession();
    return (Users) session.get("user");
}

But, when i change userService.getCurrentUser() to be like this, error is resolved but i think this is not a right manner, because it need connection to database every time i use current user.

@Override
public Users getCurrentUser(){
    session =  ActionContext.getContext().getSession();
    return daoManager.getDetailUser(((Users) session.get("user")).getUsername());
}

DaoManager.getDetailUser is like this

@Override
public Users getDetailUser(String username) {
    try {
        Users user = (Users)sessionFactory.getCurrentSession().createQuery("SELECT U FROM Users U WHERE U.username=:USERNAME")
             .setParameter("USERNAME", username)
             .uniqueResult();
        return user;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

is there any other better way to solve this problem? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:07:09+00:00Added an answer on June 1, 2026 at 11:07 pm

    The easiest way to resolve this is just to access the lazy fetched field before the session is closed:

    @Override
    public Users getCurrentUser(){
        session =  ActionContext.getContext().getSession();
        Users user = (Users) session.get("user");
        user.getRole(); //Accessed to resolve lazy field
        return user;
    }
    

    I do not recommend FetchType.EAGER. With that you cannot control access, it is always fetched whether you need it or not.. Add a few EAGER fields to your data model and suddenly your fetching the entire database for the simplest requests..

    For queries you can also use JOIN FETCH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Current Setup: So, I can use the following code to save a file
I want to save some global vars to use in the website, like current
I've got a problem with Zend_Session. I need to know, if the Session for
i want to save current date+time into database and also want to retrieve it.
window.location.href does not work to save the current web page url as a bookmark.
I wonder if there is a plugin that is able to save the current
Please suggest me a method to save an XML file to the current installation
i save in mysql database the time using NOW() function. how can i change
I use authlogic for authentication and paperclip for handling user's profile picture attachments. I
I did same as show in authlogin example . User is created successfully and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.