How could intellij changed Hibernate lazyload rules with breakpoint?

已回答

I figured out a small breakpoint on a specific line can change hibernate rules... I wonder how it could happen without my permission!

(You can reproduce this issue on sample repo

 

# Given

There are three entities: Post, Comment, User.

Post : Comment = 1 : N
Comment : User = N : 1

So each comment is connected to 1 post and 1 user. A user or a post can have multiple comments.

I set bidirectional relation with JPA/Hibernate annotations and set fetch type as LAZY load on every relation (@OneToMay, @ManyToOne) 

Lastly, I created sample entities: post A, user B, and 1 comment, which is connected to post A and user B.

 

# When

query post A, and try to read A's comments. the method looks like this: 

 

# Then (Normal Behavior)

When I run my SpringBoot app without any debug setup, these are the printed console logs: (I edited a little for readability)

START---------------------------------
Hibernate: select p.id, p.name from post p
NAME________________________________________post_A
Hibernate: select c.post_id, c.id, c.id, c.post_id, c.text, c.user_id from comment c where c.post_id=?
COMMENTS________________________________________[com.example.test2.test.Comment@410f89]
 
# Then (Weird Behavior)
 
As shown above screenshot, when I add a breakpoint to `com/example/test2/test/MyService.java:43` and run the app in debug mode, logs are changed: 
 
START---------------------------------
Hibernate: select p.id, p.name from post p
Hibernate: select c.post_id, c.id, c.id, c.post_id, c.text, c.user_id from comment c where c.post_id=?
NAME________________________________________post_A
COMMENTS________________________________________[com.example.test2.test.Comment@410f89]

Does anyone know why this happen?

 

# More weird thing

This symptom is not consistent, because when I tried with Comment instead of Post, everything works as expected. 

I think Collection types have something to do with this case... but not sure.

1

Hi Roeniss2 It looks like the IDE executes the line below System.out.println("NAME" + p.name); which leads to lazy comments initialization.

Please try to mute renderers and restart the debugger.

1

Egor Klepikov, Thank you. `mute renderer` wat the key that I needed. Problem solved.

0

请先登录再写评论。