org.hibernate.LazyInitializationException: could not initialize proxy - no Session

前端 未结 4 471
慢半拍i
慢半拍i 2020-12-31 12:17

I have 2 physical servers which my web application hits managed by load balancers. I always get -

org.hibernate.LazyInitializationException: could not initia

相关标签:
4条回答
  • 2020-12-31 12:24

    Is that an association or property - if it is a property then the issue might be that one of the server's is running a non instrumented version.

    Lazy attribute fetching: an attribute or single valued association is fetched when the instance variable is accessed. This approach requires buildtime bytecode instrumentation and is rarely necessary.
    
    0 讨论(0)
  • 2020-12-31 12:37

    From the tags you provided, I deduce you ran into this problem using Spring Framework. I ran into the same LazyInitializationException while using a Spring Data org.springframework.data.jpa.repository.JpaRepository.

    I solved the problem by annotating the method which indirectly uses Hibernate to retrieve data from the database with @Transactional.

    0 讨论(0)
  • 2020-12-31 12:40

    It sounds like the column you are trying to access is configured as an association of some sort in your entity (OneToMany, ManyToOne, whatever) and you are not populating that association in your DAO. Then, when you try to access that column (in a location in your code where there is no Hibernate Session), its not populated, Hibernate tries to load it, and boom.

    Since you are in fact using the data in that association, making it EAGER sounds like something you actually would want to do. And if that table is so large, you should look at indexing it so that queries against it are efficient.

    0 讨论(0)
  • 2020-12-31 12:40

    Your object is detached. You need to re-attach it to the current session before accessing it:

    session.update(object);
    

    Also make sure you access it within a transaction

    Read more about the problem/solution here

    0 讨论(0)
提交回复
热议问题