• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

failed to lazily initialize a collection of role could not initialize proxy - no Session

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, everyone. I got an error: failed to lazily initialize a collection of role: com.kata.cinema.base.models.entitys.Question.medias: could not initialize proxy - no Session.
I see that question is not added to media correctly because of relation between them is may-to-many. What is wrong?










 
Saloon Keeper
Posts: 27816
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless I'm mistaken, your Entity has been detached from its EntityManager (session). When you do that and you have lazy-fetched properties, the properties will return a proxy object rather than the property collection so that you will know that the property values in the collection had not been fetched.

The cure for that is to re-attach (merge) the parent Entity and fetch them. Note that the Entity returned from merge() is NOT the same ("==") Entity as the argument to merge(), although it is "equals()" to that Entity object. Use the returned Entity and discard the original instance.

You can also avoid this problem by force-fetching the lazy properties before detaching.
 
svetlana ilina
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean using query in Service?


Would you show an example "by force-fetching the lazy properties before detaching"?
 
Tim Holloway
Saloon Keeper
Posts: 27816
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm. I just realized you have a many-to-many relationship here. In a case like that, you're probably going to have to force-fetch join table and terminal objects.

Many/Many relationships are messy at the best of times and sometime the best solution is to use an alternative to SQL databases. For example, the Neo4J database is very good when the relationships between objects is as important or even more important than the entity object properties. It's quite a bit different from SQL, but complex object relationships are easy, especially many-to-many relationships. There is Spring framework support for Neo4J as part of the Spring Data module that also supplues JDBC and JPA SQL database support.

In answer to the latest quesion, though:

To force-fetch a lazy property. just access that property BEFORE detaching from the database. You should be able to simply use the collection's "size()" method, but uf not, try to fetch the first element of the collection and be prepared to see that the collection might be empty depending on what data has been stored so far.

As I said, it's messier in a many-to-many setup, but the force-fetching mechanisms are similar.
 
svetlana ilina
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used a tutorial (https://www.bezkoder.com/jpa-many-to-many/) with success (https://github.com/svil1502/spring-boot-many-to-many3.git) and this code is working


And my problem started when I had added Repository to Service, code has crashed. What is wrong? service was supposed to be a layer and not a hindrance...
 
svetlana ilina
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's done with FetchType.EAGER
I found https://stackoverflow.com/questions/61675882/lazy-initialization-manytomany-what-is-the-error, but i have no idea haw to use it in my case
 
Bartender
Posts: 2419
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Svetlana,
I ran your code from your Github you provided, I had an issue:
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property 'findMediaWithAnswers' found for type 'Media'

You may want to remove findMediaWithAnswer() method declaration from MediaRepository first.
 
svetlana ilina
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. I saw and fixed https://github.com/svil1502/spring-boot-many-to-many-work. I try to do with Eager. But I was told  fix my code with  join featch. I am looking for tutorials about it.
 
svetlana ilina
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know haw to use join fetch, if I need to add Question to Media with table media_question, not select.
 
Himai Minh
Bartender
Posts: 2419
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not too familiar with FetchMode. But you may find a clue :  https://www.baeldung.com/hibernate-fetchmode
 
Tim Holloway
Saloon Keeper
Posts: 27816
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himai Minh wrote:I am not too familiar with FetchMode. But you may find a clue :  https://www.baeldung.com/hibernate-fetchmode



The 5-second intro to FetchMode (NOTE Hibernate and JPA are NOT always the same thing!!!)

FetchMode controls how related objects are fetched from the database when you fetch an Entity object.

EAGER fetch means then if I have an Invoice Entity and a bunch of InvoiceItems (collection) and I fetch the Invoice, the InvoiceItems get fetched at the same time.

LAZY fetch means that if I fetch the Invoice, the InvoiceItems do NOT get fetched. Only if I explicitly access the InvoiceItems collection will cause them to be fetched. That means I can save time and resources if I want only the Invoice and not its line items. It also means that I avoid the dread "vacuum" effect that I'll explain in a moment.

The biggest "gotcha" to LAZY fetch comes when you fetch the Invoice and then DETACH it from its EntityManager. This is a useful thing to do, since it converts the Entity instance into a POJO that isn't secretly dragging around a lot of data management stuff with it, but the downside of not having that secret management stuff is that any attempt to access InvoiceItems will throw an Exception because the database cannot be accessed. The Proxy Object ensures that so that we can tell non-fetched data from non-existent data (that is, if there ARE no InvoiceItems).

To avoid that Exception, the Entity must first be re-attached (merged) and then you can access InvoiceItems in the normal way.

OK, to wrap it up, aside from the memory/CPU overhead, there's another disadvantage to EAGER fetching. If the dependent objects refer in turn to other objects and those references are also EAGER, then they get piggybacked in automatically (the "vaduum effect"). In some cases, a simple one-record fetch can end up pulling the entirety of a very large database into RAM.

Which is why it's useful to have a choice of fetch options, even though it does make things slightly more complicated.
 
svetlana ilina
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem was fixed with @Transactional, and LAZY fetch  was on its place. Thank you very much.

 
Himai Minh
Bartender
Posts: 2419
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Svetlana,
Congratulations. Do you specify any entity with Lazy fetch type ?
 
svetlana ilina
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, two entities Media and Question  are related  with many-to-many



 
reply
    Bookmark Topic Watch Topic
  • New Topic