Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Easy example.

Table Resource. Table Line_A. Table Line_B.

Join-Table Resource_Line_A Join-Table Resource_Line_B

Resource for Line_A could not be attached to Line_B and vice versa.

If I want all resource attached to Line_A for year 2021, in native sql I can write

Select * from Resource r join Resource_Line_A rla on r.id = rla.resource_id join Line_A la on rla.line_a_id = la.id where la.year = 2021

In Java, with repository, I've lineARepository.findByYear(2021), but Hibernate show me this query

Select * from Resource r join Resource_Line_A rla on r.id = rla.resource_id join Line_A la on rla.line_a_id = la.id join Resource_Line_B rlb on r.id = rlb.resource_id join Line_B lb on rlb.line_b = lb.id where la.year = 2021

How I can avoid the useless join on Line_B table? All relationship are fetchtype.Lazy


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
307 views
Welcome To Ask or Share your Answers For Others

1 Answer

I guess you are using a @OneToOne(mappedBy = "..", fetch = LAZY) for lineB? If so, you have to know that Hibernate 5.3 has a bug that was fixed in 5.4 which caused such associations to be eagerly fetched.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...