Forums Register Login

Hibernate fetching eagerly when told to fetch lazy

+Pie Number of slices to send: Send
So I have been stuck with this problem for a few days where Hibernate fetches all child objects when coded not to load them eagerly.




This is the code I use to retrieve the namespace:


By inspecting Namespace.urls with IntellIJs debugger, I see that it's fully initialized. This is bad.
URL.namespace for example is not initialized, which is as expected.
+Pie Number of slices to send: Send
 

P Marksson wrote:By inspecting Namespace.urls with IntellIJs debugger, I see that it's fully initialized. This is bad.
URL.namespace for example is not initialized, which is as expected.


And what is the value of the boolean parameter eager?
+Pie Number of slices to send: Send
Lazy loaded data is only loaded when needed.
Does inspecting a variable with the debugger constitute a request for the data - which would cause it to be loaded?




+Pie Number of slices to send: Send
 

Roel De Nijs wrote:

P Marksson wrote:By inspecting Namespace.urls with IntellIJs debugger, I see that it's fully initialized. This is bad.
URL.namespace for example is not initialized, which is as expected.


And what is the value of the boolean parameter eager?



Of course



Stefan Evans wrote:Lazy loaded data is only loaded when needed.
Does inspecting a variable with the debugger constitute a request for the data - which would cause it to be loaded?



Almost certain that the debugger wont cause Hibernate to fetch the data, since it's using either reflection or the dubbuger API to view data and not the getters(which triggers the database call).
For example, when fetching an URL with the dao, by inspecting it with the debugger, we can see that URL.Namespace is not null, but all its fields are. When we explicitly call a getter, Hibernate fetches the data for the given field.
+Pie Number of slices to send: Send
I think Cascade.ALL for URL.Namespace is the root to this evil. But there is a reason why I have it.

First, this is how the saving is done:

SQL Log:

As you see, Hibernate tries to resave the parent entity, even if it already exists. Adding Cascade.All/SAVE_UPDATE seems to resolve this.

Normally, we use java.lang.Long as primary key. Auto generated. This means, if the ID is null, Hibernate interpret it as a new entity. Thus, doing an insert operation. If the ID on the other hand is already set, an update operation is performed(or nothing at all).

If the primary key is a string/varchar, Hibernate seems to interpret it as a new entity no matter what. Thus, always doing an insert operation. Which in turn cause my MySQLIntegrityConstraintViolationException.

Have no idea how to resolve this though.
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 594 times.
Similar Threads
one to one mapping problem
embedded class question
How to avoid this hibernate jpa LazyInitializationException exception in Swing application?
Lazy loading in One-To-One association
HIbernate Mapping problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:35:16.