6

When im stepping over hibnerate objects with Intellij the objects appear to be null in the debugging window. Labelled as $$_hibernate_interceptor

So lets say I have some simple code that looks like the following

Subscription subscription  = subscriptionRepository.getOne(1l);
log.info("add breakpoint debug code stopped here")

I would like to see what values that have been returned from the subscriptionRepository and assigned to the subscription object within the the Debug: window.

See screenshot that shows what this looks like on my computer.

enter image description here

As you can see most of the Variable values appear to be empty, however if I do a toString() on the subscription object it will output values that are marked as null in the inteceptor window. So they are not really empty.

So couple of questions here

  • What is $$_hibernate_interceptor

Further findings

The interceptor is only returned when I use the default repository.getOne. If I use findBy or my own findByField it does not call the interceptor and displays the values in the debug window.

What is happening here why does getOne display interceptor but the others dont?

Robbo_UK
  • 11,351
  • 25
  • 81
  • 117
  • 1
    `getOne()` returns an unitilialized proxy. That's the whole point of that method. Calling a method of that proxy (like toString()) is what will initialize the proxy, i.e. tell Hibernate to execute the query and load the data from the database. So what you see is perfectly normal and expected. That happens through byte-code generation, and the bytebuddy interceptor is (AFAIK) the object that will intercept the method calls and initialize the proxy. – JB Nizet Dec 08 '18 at 18:28
  • Thabks for feedback makes sense. So what would be my options to view the object in the debug panel? – Robbo_UK Dec 08 '18 at 22:22
  • 1
    I guess you could use the "evaluate expression" tool to call `toString()`? https://www.jetbrains.com/help/idea/evaluating-expressions.html – Joe Halliwell Dec 17 '18 at 10:35

1 Answers1

4

As you can see, the type of your Subscription is Subscription$HibernateProxy$.... This is because Hibernate is lazy-loading properties from the database as you use them.

Until the first time they are accessed, say, in a toString(), they will be null. You can sidestep this easily by adding a variable watch (the little + icon) on "subscription.toString()".

The difference between findById and getOne is that the former returns a lazy proxy, while the latter uses an eagerly loaded object. Some information on why can be found here.

cfstras
  • 1,613
  • 15
  • 21