0

I am using Hibernate in a spring-boot 2.1.0 setup

I have two JPA entities linked through an @ManyToOne(fetch = FetchType.LAZY),annotation.

when I lookup the first entity through a named query, I can see that hibernates executes followup queries to populate the linked entity. How do I suppress that?

Kai
  • 1,767
  • 1
  • 17
  • 31
  • How can you see that? When do you see that? What are you doing with the first entity once it's loaded? – JB Nizet Nov 27 '18 at 23:29
  • I enabled show_sql in the hibernate properties.I actually can't do anything with the object at the moment because, the second, unintended query fails for some database issue. – Kai Nov 27 '18 at 23:37
  • Good. That answers my first question. Now what about the two other ones? – JB Nizet Nov 27 '18 at 23:37
  • I trigger the code executing the query. I see the hibernate queries on the console. I get an SqlException from the second query that should not have happened if Hibernate would have respected Fetch=Lazy – Kai Nov 27 '18 at 23:39
  • So you just execute the query, and you do absolutely nothing with the returned list? The method ends after you've executed the query and ignored its result? You don't iterate it, you don't serialize it as XML or JSON, you don't inspect its content with a debugger, nothing at all? Post the code you're executing, the code of the entity, and the SQL queries – JB Nizet Nov 27 '18 at 23:42
  • my code calls `query.getSingleResult() ` which then fails with SQLException before I get hold of my entity. The reason is that Hibernate triggered to load the second entity which is ManyToOne linked automatically within that stack and that is what I want to turn off. That the second query currently fails has to do with a completely different issue (hibernate ignoring the maximum of characters a column alias can have) – Kai Nov 27 '18 at 23:50
  • I think I found a similar question now to this: https://stackoverflow.com/questions/42625485/hibernate-ignores-lazy-fetch-type-and-loads-properties-immediately – Kai Nov 27 '18 at 23:55
  • This question is about a OneToOne mapped by the other side of the association. Your question is about a ManyToOne. Unless of course your description doesn't match your actual code, but we can only make conjectures, because you didn't post it. Post all the details needed to understand and diagnose the problem (code, stack trace, SQL queries). – JB Nizet Nov 28 '18 at 00:00
  • I tried it and had no problem so something else is going on. JPA respects the `fetch=FetchType.LAZY` just fine for me. Hibernate version 5.2.1.Final. Not to mention if you are using spring-boot you are probably using spring-data-jpa and not jpa so maybe you need to clarify your question. Vote to close. – K.Nicholas Nov 28 '18 at 00:17
  • @K.Nicholas spring-data-jpa is just a thin layer on top of JPA. If you're using spring-data-jpa, you're using jpa. But I agree with the vote to close: without code, we can't help. – JB Nizet Nov 28 '18 at 00:22
  • @JBNizet - understood. There are some differences between them and spring-data-jpa likes to do so much for you I wouldn't be surprised if it trampled over the lazy fetch settings for a unidirectional fetch. I don't think it is all that thin of a layer but regardless I didn't test against spring-data-jpa. – K.Nicholas Nov 28 '18 at 00:51

0 Answers0