In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to solve the problem of JPA @ OneToMany and invalid lazy loading". In daily operation, I believe many people have doubts about how to solve the problem of JPA @ OneToMany and invalid lazy loading. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to solve the problem of JPA @ OneToMany and lazy loading invalid"! Next, please follow the editor to study!
Catalogue
Invalid JPA @ OneToMany and lazy loading
@ OneToMany
Make a brief summary.
Implement lazy loading and no foreign keys of JPA
For example
Use when converting
Invalid JPA @ OneToMany and lazy loading
@ OneToOne @ ManyToMany is used without too much explanation, focusing on solving the "lazy load invalid problem".
Example:
@ OneToMany
Teacher and student have an one-to-many relationship.
You only need to use the @ OneToMany annotation on studentList, and the corresponding parameters are lazy loading, cascading operations, and subtable foreign keys.
In order to verify that lazy loading works, I found that lazy loading did not work in debug mode. In normal mode, there is also studentList data returned to the page. So he began to drain the pit and gradually doubted life.
Until I saw such a sentence from an international friend.
It seems to be a debugging artifact.
At debugging time, because the transaction is still open, the watched lazy loaded entity properties will be loaded at the breakpoint evaluation time.
So add spring.jpa.show-sql=true to the application.properties to open the executed SQL.
Under debug, execute to line 29 and execute the following two sentences of SQL:
Hibernate: select teacher0_.id as id1_1_0_, teacher0_.age as age2_1_0_, teacher0_.name as name3_1_0_ from teacher teacher0_ where teacher0_.id=?Hibernate: select studentlis0_.teacher_id as teacher_4_0_0_, studentlis0_.id as id1_0_0_, studentlis0_.id as id1_0_1_, studentlis0_.addr as addr2_0_1_, studentlis0_.name as name3_0_1_ Studentlis0_.teacher_id as teacher_4_0_1_ from student studentlis0_ where studentlis0_.teacher_id=?
At first, only the teacher table was queried, followed by an associative query, which, combined with the above sentence, speculated that it might be caused by debug. When starting in normal mode, there are also two SQL, and guess that when the front end is returned, the serialization automatically calls the getStudentList () method, resulting in the execution of the second SQL.
So create a new TeacherDto.class
And return teacherDto in controller, not returning teacher directly.
When it starts in normal mode, there is only one SQL and there is no cascading query.
Hibernate: select teacher0_.id as id1_1_0_, teacher0_.age as age2_1_0_, teacher0_.name as name3_1_0_ from teacher teacher0_ where teacher0_.id=?
At this point, the stampede is over.
Make a brief summary.
When using @ OneToOne, @ OneToMany, @ ManyToMany, you only need to add the parameter fetch = FetchType.LAZY.
In debug mode, cascading queries are automatically performed, resulting in invalid lazy loading, which may be set up deliberately by idea for developers to debug.
When the interface returns, avoid returning entity directly, but return Dto or Vo.
Implement lazy loading and no foreign keys of JPA
Found a lot of lazy loading of jpa on the Internet, either crawling strategy or casually adding a fetch=FetchType.LAZY
In fact, jpa is very simple to implement lazy loading. In fact, it is the same as mybatis, except that you do not call the get method of the corresponding property.
For example
When many interfaces output objects, they use BeanUtils.copyProperties () to convert entities into dto output, and then use its overloaded method copyProperties (Object source, Object target, String... IgnoreProperties), lazy loading can be realized.
The code is as follows
Public class NoticeRecord {@ OneToMany (fetch=FetchType.LAZY) @ JoinColumn (name = "noticeId", foreignKey = @ ForeignKey (name = "null")) private List noticeSendeeRecords;} is used when converting
The purpose of this overloaded method is that the transformation ignores the noticeSendeeRecords attribute in noticeRecord
BeanUtils.copyProperties (noticeRecord,noticeRecordDTO, "noticeSendeeRecords")
In this way, the lazy loading of jpa is realized. Check the output sql statements. There are only statements for querying NoticeRecord, but no statements for querying NoticeSendeeRecord.
Instead of letting jpa generate foreign keys, use foreignKey = @ ForeignKey (name = "null").
At this point, the study on "how to solve the problem of JPA @ OneToMany and invalid lazy loading" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.