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 6175481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:55:57+00:00 2026-05-23T23:55:57+00:00

I tried this: @RequestMapping(method = RequestMethod.GET, value = /getmainsubjects) @ResponseBody public JSONArray getMainSubjects( @RequestParam(id)

  • 0

I tried this:

@RequestMapping(method = RequestMethod.GET, value = "/getmainsubjects")
@ResponseBody
public JSONArray getMainSubjects( @RequestParam("id") int id) {

List <Mainsubjects> mains = database.getMainSubjects(id, Localization.getLanguage());
JSONArray json = JSONArray.fromObject(mains);
return json;

}

When calling getmainsubjects.html?id=1 I get the error:

net.sf.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: fi.utu.tuha.domain.Mainsubjects.aiForms, no session or session was closed

How to fix?

  • 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-05-23T23:55:58+00:00Added an answer on May 23, 2026 at 11:55 pm

    The problem is,
    your model object Mainsubjects had some associations (built by OneToMany, ManyToOne, etc.), Lists (PersistentBags), Sets or something (Collection) like this which’re initialized lazily. It means, after initialization of result set, Mainsubjects doesn’t point to an actual collection object, instead proxies. While rendering, accessing this collections, hibernate tries to get the values from Database using proxies. But at this point there’s no session open. For that reason you get this exception.

    You can either set your fetching strategy to EAGER (if you use annotations) like this:
    @OneToMany(fetch=FetchType.EAGER)

    In this method you must be aware, that you can not allow more than one PersistentBag initialized eagerly.

    or you can use OpenSessionInView pattern, which’s a servlet filter opens a new session before your request’s handeled by controller, and closes before your web application responses:

       public class DBSessionFilter implements Filter {
            private static final Logger log = Logger.getLogger(DBSessionFilter.class);
    
            private SessionFactory sf;
    
            @Override
            public void destroy() {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void doFilter(ServletRequest request, ServletResponse response,
                    FilterChain chain) throws IOException, ServletException {
                try {
                    log.debug("Starting a database transaction");
                    sf.getCurrentSession().beginTransaction();
    
                    // Call the next filter (continue request processing)
                    chain.doFilter(request, response);
    
                    // Commit and cleanup
                    log.debug("Committing the database transaction");
                    sf.getCurrentSession().getTransaction().commit();
    
                } catch (StaleObjectStateException staleEx) {
                    log.error("This interceptor does not implement optimistic concurrency control!");
                    log.error("Your application will not work until you add compensation actions!");
                    // Rollback, close everything, possibly compensate for any permanent changes
                    // during the conversation, and finally restart business conversation. Maybe
                    // give the user of the application a chance to merge some of his work with
                    // fresh data... what you do here depends on your applications design.
                    throw staleEx;
                } catch (Throwable ex) {
                    // Rollback only
                    ex.printStackTrace();
                    try {
                        if (sf.getCurrentSession().getTransaction().isActive()) {
                            log.debug("Trying to rollback database transaction after exception");
                            sf.getCurrentSession().getTransaction().rollback();
                        }
                    } catch (Throwable rbEx) {
                        log.error("Could not rollback transaction after exception!", rbEx);
                    }
    
                    // Let others handle it... maybe another interceptor for exceptions?
                    throw new ServletException(ex);
                }
    
            }
    
            @Override
            public void init(FilterConfig arg0) throws ServletException {
                log.debug("Initializing filter...");
                log.debug("Obtaining SessionFactory from static HibernateUtil singleton");
                sf = HibernateUtils.getSessionFactory();
    
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just tried adding this to my sample controller: @RequestMapping(value=/jsontest, method=RequestMethod.GET) public @ResponseBody User
This is my code: @Controller @RequestMapping(loginform.htm) public class LoginController { @RequestMapping(method = RequestMethod.GET) public
code is like this: @Controller public class TestController { @RequestMapping(value = /testerror, method =
Here's how my method looks like: @RequestMapping(value = /form, method = RequestMethod.POST) public String
I tried this earlier today: public interface IFoo { IEnumerable<int> GetItems_A( ref int somethingElse
An item is displayed at this URL: /item/10101 using this Controller method: @RequestMapping(value =
I tried this, but could not get through:- code behind protected HtmlTableRow trComment; protected
I tried this on my own but couldn't get it to work right. Basically
Tried this in as connection string connectionString=Server=.\SQLEXPRESS; AttachDbFilename=E:\Database\dnn49.mdf;Database=dnn49; Trusted_Connection=Yes; but i get an error
I tried this: Console.WriteLine(Convert.ToBase64String(Encoding.UTF8.GetBytes(hi))); Console.WriteLine(Convert.ToBase64String(Encoding.UTF8.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes(hi))))); and I get different results for them, although I

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.