Search code examples
javajsonhibernaterestful-url

Hibernate :HTTP Status 500 - Internal Server Error


below is my hibernate code:

SessionFactory sessionFactory;
        Session session = null;
        LoginEntity user = null;

        try {

            sessionFactory = HibernateUtility.getSessionFactory();
            session = sessionFactory.openSession();
            session.beginTransaction();

            user = session.get(LoginEntity.class, user_id);

            System.out.println(user.getUserCountryMapping()); // if I remove this line I get error..

            session.getTransaction().commit();
            return user;

        } catch (Exception e) {
            Logger.getLogger(BulkActionMethods.class.getName()).log(Level.SEVERE, null, e);
        } finally {
            if (session != null) {
                session.close();
            }
        }

I am facing a weird issue with my code, When I remove the System.out.println(user.getUserCountryMapping()); line I am getting HTTP Status 500 - Internal Server Error error on browser but when I write this line I am getting expacted JSON response on browser.. Please someone help me to understand this issue.


Solution

  • Replace

    System.out.println(user.getUserCountryMapping());

    with

    Hibernate.initialize(user.getUserCountryMapping());

    Read more about lazy vs eager fetch type.