当前位置:网站首页>Analysis of cascading relation operation examples of cascade
Analysis of cascading relation operation examples of cascade
2022-07-25 03:17:00 【Yisu cloud】
cascade Cascading relationship operation example analysis
Today I'd like to share with you cascade Relevant knowledge points of cascading relationship operation example analysis , Detailed content , Clear logic , I believe most people still know too much about this knowledge , So share this article for your reference , I hope you will gain something after reading this article , Now let's take a look .
attribute
CascadeType.REFRESH: Cascade refresh , When multiple users operate on an entity at the same time , The data obtained for the user is real-time , Before using the data in the entity, you can call refresh() MethodCascadeType.REMOVE: cascading deletion , When calling remove() Methods to remove Order Entities will be cascaded and deleted first OrderItem Relevant dataCascadeType.MERGE: update cascade , When calling the Merge() Method , If Order The data in will be updated accordingly OrderItem Data inCascadeType.PERSIST: Cascade save , When calling the Persist() Method , The corresponding data will be cascaded and savedCascadeType.DETACH: Cascade de tube / Free operation , If you want to delete an entity , But it has foreign keys that cannot be deleted , You need this cascade permission . It will undo all related foreign key associations .
CascadeType.ALL: Contains all of the above cascading properties
Cascade save , Cascade modification , Cascade delete cases
// School @Table(name = "t_school")@[email protected]@Setterpublic class School extends BaseEntity { private String name; @JsonIgnore @OneToMany(mappedBy = "school", cascade = {CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST}, fetch = FetchType.LAZY) private List<Student> students = new ArrayList<>(); // Manually construct the method of adding students public void addStudent(Student stu){ if(!students.contains(stu)){ stu.setSchool(this); // Set up school this.students.add(stu); // add to } } // rewrite equals and hashCode Method @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; School school = (School) o; return Objects.equals(name, school.name) && Objects.equals(students, school.students); } @Override public int hashCode() { return Objects.hash(name, students); }}// Students @Table(name = "t_student")@[email protected]@Setterpublic class Student extends BaseEntity { private String name; private String age; @ManyToOne(cascade = CascadeType.REFRESH,optional = false) @JoinColumn(name = "SCHOOL_ID") private School school;}// Test class @RunWith(SpringRunner.class)@SpringBootTestpublic class SchoolDaoTest { @Autowired private SchoolDao schoolDao; @PersistenceContext private EntityManager em; @Test public void testSave() { School school = new School(); school.setName(" School 1"); Student student1 = new Student(); student1.setAge("18"); student1.setName(" Zhang San "); Student student2 = new Student(); student2.setAge("18"); student2.setName(" Li Si "); school.addStudent(student1); school.addStudent(student2); // After testing , use save The method is also possible schoolDao.saveAndFlush(school); // This method dao Class needs to inherit JpaRepository } @Test public void testDel() { schoolDao.deleteById(1365157538168864L); } /** * update cascade */ @Test public void testMerge(){ Optional<School> rs = schoolDao.findById(1365164498616352L); School school = rs.isPresent()? rs.get() : null; if(school != null){ school.setName(" School 2"); List<Student> students = school.getStudents(); if(students != null && students.size() > 0){ for (Student student : students) { student.setName(" Change the name "); } } school.setStudents(students); schoolDao.save(school); } }} After testing , In preservation school When we get together student Keep it together , Cascade save Deleting schools will cascade student Delete cascading updates together `cascade` Attribute plus `CascadeType.MERGE`, Be careful : Cannot be used with lazy loading , Correct loading method :fetch = FetchType.EAGER
That's all “cascade Cascading relationship operation example analysis ” All the content of this article , Thank you for reading ! I believe you will gain a lot after reading this article , Xiaobian will update different knowledge for you every day , If you want to learn more , Please pay attention to the Yisu cloud industry information channel .
边栏推荐
- Eslint error
- C. Mark and His Unfinished Essay
- Web -- JDBC tool class writing
- Interview question -- event cycle
- Resolve the error: org.apache.ibatis.binding.bindingexception
- Modulenotfounderror: no module named 'pyemd' solution
- Learning record XIII
- Easyexcel sets the style of the last row [which can be expanded to each row]
- Beginners must see the markdown User Guide
- Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
猜你喜欢

Record once C # extract audio files with ffmempeg

Learning record XIII
![Easyexcel sets the style of the last row [which can be expanded to each row]](/img/25/8fb41f222cb3cca0119515d070b667.png)
Easyexcel sets the style of the last row [which can be expanded to each row]

B. Making Towers

Unified return data format

mysql_ Case insensitive

JS foundation -- math

Riotboard development board series notes (VII) -- the use of framebuffer

How does Jupiter notebook change themes and font sizes?

Selenium framework operation steelth.min.js file hides browser fingerprint features
随机推荐
Query the information of students whose grades are above 80
Hal library serial port for note taking
A. Subtle Substring Subtraction
Introduction to Apache Doris grafana monitoring indicators
Reasons for not sending requests after uni app packaging
Learning record Xi
mysql_ Master slave synchronization_ Show slave status details
C language function operation
Question D: pruning shrubs
List title of force buckle summary
Leetcode programming practice -- Tencent selected 50 questions (I)
Banana pie bpi-m5 toss record (3) -- compile BSP
Learning record XIII
Solve mysql5.6 database specified key was too long; Max key length is 767 bytes
Sum of "n" numbers of force deduction summary
Resolved (the latest version of selenium reported an error) attributeerror: module 'selenium webdriver‘ has no attribute ‘PhantomJS‘
Enter an integer and a binary tree
Method of adding kernel in Jupiter notebook
JS foundation -- object static method
JS password combination rule - 8-16 digit combination of numbers and characters, not pure numbers and pure English