当前位置:网站首页>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 .
边栏推荐
- Use go language to import Doris data through stream load
- Learning record 10
- Use reflection to convert RDD to dataframe
- Hashcode details
- 10. 509 Certificate (structure + principle)
- Hal library serial port for note taking
- Dc-1-practice
- Resolve the error: org.apache.ibatis.binding.bindingexception
- C language writes a circular advertising lantern or changes it to a confession system
- [stm32f103rct6] motor PWM drive module idea and code
猜你喜欢

Riotboard development board series notes (6) -- buildreoot building system image

JS foundation -- object static method

How to use two stacks to simulate the implementation of a queue
![Matplotlib tutorial (I) [getting to know Matplotlib first]](/img/dc/e7dfc435900c14e3e9be07f6ad75fe.png)
Matplotlib tutorial (I) [getting to know Matplotlib first]

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

JS foundation -- hijacking of this keyword

Operator explanation - C language

Consistent hash, virtual node, bloom filter

JS foundation -- JSON
![[Kali's sshd service is enabled]](/img/1b/180534d51049177254e30c4b783eba.png)
[Kali's sshd service is enabled]
随机推荐
Color space (1) - RGB
05 - MVVM model
mysql_ User table_ Field meaning
Enter an integer and a binary tree
Calculation method of confusion matrix
Hyperchain hyperblockchain Shi Xingguo was interviewed by 36 krypton: the amount of customer cooperation consulting is doubling
Consistent hash, virtual node, bloom filter
How chemical enterprises choose digital service providers with dual prevention mechanism
Riotboard development board series notes (VII) -- the use of framebuffer
JS written test question -- promise, setTimeout, task queue comprehensive question
JS common interview questions
Question D: pruning shrubs
Page performance: how to optimize pages systematically?
Canvas record
DOM operation -- get elements and nodes
Solve ''_ Xsrf 'argument missing from post
mysql_ Master slave synchronization_ Show slave status details
Learning Record V
JS written test question -- deep copy of object
# CF #807 Div.2(A - D)