Error Could Not Initialize Proxy - No Session
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers could not initialize proxy no session lazy loading or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x nhibernate could not initialize proxy - no session Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it could not initialize proxy - no session grails only takes a minute: Sign up Could not initialize proxy - no Session up vote 24 down vote favorite 13 I've got an error that looks like this: Could not initialize proxy - no Session I'm working with java,
Could Not Initialize Proxy - No Session Spring
hibernate and spring. This error comes up when trying to generate a PDF document, and I'm following the next steps to generate it on the fly and store in the database. I sent a request to the app through a POST method. This generates the PDF on the fly and shows to the user. Just after that request I send another, but through an ajax a request. This will generate the same PDF but will save it in the DB. could not initialize proxy - no session hibernate The error shows that a query could not be executed due to "could not initialize proxy - no Session" error. Is there something that am I doing wrong, calling the same methods twice from the same user session? Could it be that the session is closed before both requests have finished? Hope someone can help me to understand what is happening. java ajax spring hibernate session share|improve this question edited May 25 '13 at 19:24 asked May 25 '13 at 18:42 Misael P 1721110 Error aside, why would you make two distinct calls to create the PDF? Do it once, persist it, then retrieve it from the database instead. –Makoto May 25 '13 at 19:01 When I was writing the question I thought the same you are suggesting. But I wanted that someone with a bit more experience confirm the same. And just to make it more useful... Can you explain why the "could not initialize proxy - no Session" error happens in this specifi scenario?. thanks in advance. –Misael P May 25 '13 at 19:08 Hibernate issue. Show us the code behind the Ajax call so we can we can help you. –Jukka May 25 '13 at 20:18 add a comment| 1 Answer 1 active oldest votes up vote 60 down vote accepted Your problem is that the hibernate Session lives only for one request. It opens in the start
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about could not initialize proxy - no session @transactional Stack Overflow the company Business Learn more about hiring developers or posting ads with
Could Not Initialize Proxy - No Session Spring Boot
us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is
Grails Org.hibernate.lazyinitializationexception: Could Not Initialize Proxy - No Session
a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up LazyInitializationException in Hibernate : could not initialize proxy - no Session up vote http://stackoverflow.com/questions/16752799/could-not-initialize-proxy-no-session 0 down vote favorite I call dao from my service as @Override @Transactional public Product getProductById(int id) { return productDao.getProductById(id); } and in the dao I am getting product as @Override public Product getProductById(int id) { Product p = sessionFactory.getCurrentSession().load(Product.class, id); System.out.print(p); return p; } This runs fine but if I change my dao class to @Override public Product getProductById(int id) { return sessionFactory.getCurrentSession().load(Product.class, id); } I get org.hibernate.LazyInitializationException: could http://stackoverflow.com/questions/39054193/lazyinitializationexception-in-hibernate-could-not-initialize-proxy-no-sessi not initialize proxy - no Session. The exception occurs in view layer where I am just printing the product. I do not understand why returning in same line in dao method results in exception in view layer but works fine if I save it in a reference and then return that. java spring hibernate share|improve this question edited Aug 26 at 7:43 asked Aug 20 at 12:33 Turtle 84 add a comment| 2 Answers 2 active oldest votes up vote 3 down vote accepted Here's a good reference to get you familiar with how .get() and .load() method works. @Override public Product getProductById(int id) { Product p = sessionFactory.getCurrentSession().load(Product.class, id); return p; } session.load() by default returns a proxy object without hitting a database. It basically returns NoObjectFoundError if there aren't any records on the table or else it will return a reference without populating the actual object or even hitting the database. Your above method returns an proxy and since it has to initialize the your object as well, the session remains open and object is populated. @Override public Product getProductById(int id) { return sessionFactory.getCurrentSession().load(Product.class, id); } But in your second method, basically a proxy is returned without any initialization. session is closed thereafter without any
not initialize proxy - no Session in Hibernate Java If you are working in Hibernate framework, then you know that one of the key feature of Hibernate is "lazy initialization", which allows framework to http://javarevisited.blogspot.com/2014/04/orghibernatelazyinitializationException-Could-not-initialize-proxy-no-session-hibernate-java.html lazily initialize dependencies, relationship or association lazily from database on need basis. For example, if you are dealing with User object, which has relationship with Permission object like one user can have http://forum.spring.io/forum/spring-projects/data/31409-hibernate-spring-could-not-initialize-proxy-no-session multiple permissions, then Hibernate may choose not to initialize the collection which holds all permissions at the time it initialized User object and instead returns a proxy object. At this point, if you could not close your session and letter tries to access an attribute from Permission object, you will get "org.hibernate.LazyInitializationException: could not initialize proxy - no Session in Hibernate". Why this error comes, because hibernate needs to go database to initialize the proxy object, and connection is already closed. If you remember, what we discussed in difference between get vs load in hibernate that Proxy object is only initialized could not initialize in Hibernate if you access any attribute other than id itself, that's why you would only see LazyInitializationException if you try to access an attribute other than id. In this article, we will see different scenarios on which you could possibly get "org.hibernate.LazyInitializationException: could not initialize proxy - no Session in Hibernate" and how to solve them appropriately. I have tried to explain reasons which caused the error, and explained the solution as why it will work, but if you still face issues, then feel free to post it here. By the way, good understanding of lazy initialization is also a good Hibernate interview question, so this not only help you to solve this error but also to do well during interviews. 1) Code tries to access a lazy initialized property or collection and session is not available. This is by far most common reason of "LazyInitializationException: could not initialize proxy". In order to find the reason you need to look your code carefully. Here is one example to understand, how lazy initialization exception comes in Hibernate : Session s = sessions.openSession(); Transaction tx = s.beginTransaction(); Employee e = (Employee) s.createQuery("from Employee e where e.name
and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor. Announcement Announcement Module Collapse No announcement yet. Hibernate/Spring "could not initialize proxy-no Session Page Title Module Move Remove Collapse X Conversation Detail Module Collapse Posts Latest Activity Search Forums Page of 1 Filter Time All Time Today Last Week Last Month Show All Discussions only Photos only Videos only Links only Polls only Filtered by: Clear All new posts soulspirit Junior Member Join Date: Jun 2007 Posts: 23 #1 Hibernate/Spring "could not initialize proxy-no Session Jun 14th, 2007, 04:29 AM Hi, im trying to write a simple program in Java to read some stuff from my database. To load data from the database, I'm using the HibernateTemplate.load() function. However it doesnt work at all. First I've used the HibernateTemplate.get() function which worked perfectly, but now I want Hibernate to throw an exception if an element was not found in the database. the reference of the load function says: ObjectRetrievalFailureException - if not found DataAccessException - in case of Hibernate errors But even if I try to load a non-existing element, I dont get any exception from the load function. The exception from the stack-trace occurs when I try to read the data from the object returned from the load-function (System.io.println(dao.getManufacturer) ). Hibernate version: 3.2 Spring version: 2.0.5 Mapping documents: Mapping File: Code: