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.
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?
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?
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.
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.
Post by:autobot
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