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

  • Home
  • SEARCH
  • 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 7857295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:59:50+00:00 2026-06-02T20:59:50+00:00

Why can’t I get value of entity object product.getName() loaded by id session.load(product.class,1) after

  • 0

Why can’t I get value of entity object product.getName() loaded by id session.load(product.class,1) after method with @Transactional annotation returns? When I get product object in this way session.createQuery("from Product as product WHERE product.id = 1) everything is fine.

Edit

Dao method

public Product getProduct(Long id) {
return (Product) currentSession().load(Product.class, id);
}

Service method

    @Transactional
public Product getProduct(Long id) {
    return productDao.getProduct(id);
}

Controller method – it is supposed to send JSON but it breaks on product.getName() with error org.hibernate.LazyInitializationException: could not initialize proxy - no Session

    @RequestMapping(value = "/product",headers="Accept=application/json")
public @ResponseBody Product getProduct() {
    Product product = productService.getProduct(new Long(1));
    System.out.println(product.getName());
    return product;
}
  • 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-02T20:59:52+00:00Added an answer on June 2, 2026 at 8:59 pm

    A LazyInitializationException will be thrown by Hibernate if an uninitialized collection or proxy is accessed outside of the scope of the Session, i.e., when the entity owning the collection or having the reference to the proxy is in the detached state.

    Sometimes a proxy or collection needs to be initialized before closing the Session. You can force initialization by calling product.getName() or for example. However, this can be confusing to readers of the code and it is not convenient for generic code.

    The static methods Hibernate.initialize() and Hibernate.isInitialized(), provide the application with a convenient way of working with lazily initialized collections or proxies. Hibernate.initialize(product) will force the initialization of a proxy, product, as long as its Session is still open.

    You can also declare your some of fields to be eagely loaded

    @Entity 
    public class Product  {
    
       @Id 
       @GeneratedValue
       private long id;
    
    
       @Basic(fetch=EAGER)
       private String name;
    
    
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can find why i get this error can someone help? package Android.data; public class
Can i call a Method on the Thread Object ??
Can anyone let me know how can we change the value of kendo combobox
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can any one tell, how to get the result of LINQ query contains group
Can I register a .Net COM class with the SingleUse -flag? The reason I
Can someone please explain why this doesn't work? MyClass myClass1 = new MyClass(); object
Can anyone explain me what's going on? I use this method - (BOOL)shouldAutorotateToInterfaceOrientation:( UIInterfaceOrientation)interfaceOrientation
can anyone tell me if it is possible to convert a dojo charting object
Can i get the source code for a WAMP stack installer somewhere? Any help

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.