• 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
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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: 2421
13
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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
  • Number of slices to send:
    Optional 'thank-you' note:
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: 2421
13
  • Number of slices to send:
    Optional 'thank-you' note:
I am not too familiar with FetchMode. But you may find a clue :  https://www.baeldung.com/hibernate-fetchmode
 
Tim Holloway
Saloon Keeper
Posts: 27851
196
  • Number of slices to send:
    Optional 'thank-you' note:

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
  • Number of slices to send:
    Optional 'thank-you' note:
Problem was fixed with @Transactional, and LAZY fetch  was on its place. Thank you very much.

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



reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |