当前位置:网站首页>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 .
边栏推荐
- Bgy development small example
- Clothing ERP | ten advantages of clothing ERP for enterprises
- Question B: shunzi date
- Arduino IDE for raspberry PI Pico development firmware localization installation tutorial
- JS common interview questions
- Riotboard development board series notes (VIII) -- building desktop system
- JS foundation -- JSON
- JS foundation -- task queue and event loop
- Can bus baud rate setting of stm32cubemx
- Function of each layer of data warehouse
猜你喜欢

B. Making Towers

Riotboard development board series notes (4) -- using Vpu hardware decoding

If there is a segment in the encryption field, are you "bronze" or "King"?
![[stm32f103rct6] motor PWM drive module idea and code](/img/a5/3010acff73c8913e967ff3a024877e.png)
[stm32f103rct6] motor PWM drive module idea and code

Method of adding kernel in Jupiter notebook

Merge sort / quick sort

JS written test question -- promise, setTimeout, task queue comprehensive question

Dc-1-practice

Chrome process architecture

Define macros in makefile and pass them to source code
随机推荐
# CF #807 Div.2(A - D)
JS foundation -- JSON
Define macros in makefile and pass them to source code
Brief understanding of operational amplifier
Eslint error
Print the common part of two ordered linked lists
Time formatting
JS foundation -- object static method
File permission management
Openlayers ol ext: Transform object, rotate, stretch, zoom in
Uni app configuration
Method of adding kernel in Jupiter notebook
Learning Record V
mysql_ User table_ Field meaning
ECMAScript new features
Stm32cubemx quadrature encoder
Learning record XIII
Easyexcel sets the style of the last row [which can be expanded to each row]
Import and export using poi
Openlayers draw deletes the last point when drawing