Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate fetching eagerly when told to fetch lazy

 
P Marksson
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stefan Evans
Bartender
Posts: 1807
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
P Marksson
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply
  • Bookmark Topic Watch Topic
  • New Topic