Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

java - Jpa/Hibernate: avoid join on all join-table in an Entity that is UNIQUE per each relationship

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


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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...