Hibernate Summary (2), hibernate Summary

Source: Internet
Author: User

Hibernate Summary (2), hibernate Summary

In the previous Hibernate Summary (1) briefly summarized the simple use of level 1 cache, snapshot, add, delete, modify, and query. This article summarizes the cascade operations of the two tables.

Cascade involves three scenarios: Inter-cascade, 1-cascade, and inter-1.

  • The first is the case of 1-region, period-1, so first set a scenario: the relationship between the customer and the order
// The get ()/set () method public class Customer {private Integer id; private String name; private Set <Order> orders = new HashSet <Order> (); // One to multiple} public class Order {private Integer id; private String name; private Customer customer Customer; // multiple-to-one}

Next, the configuration files Order. hbm. xml and Customer. hbm. xml of hibernate. cfg. xml and the two entity classes are used. The first one is not to mention, mainly the configuration of the ing file.

// Customer. hbm. xml Inverse: true. The relationship is not maintained and handed over to the owner of the foreign key for maintenance.
Cascade: save-update (cascade storage and cascade update). Of course, there are other options: delete (cascade deletion), all (cascade storage, update, and delete ), none (no Cascade by default)
--> <Set name = "orders" inverse = "true" cascade = "save-update"> <key column = "cid"> </key> <! -- Name of the foreign key in the Order table --> <one-to-sequence class = "Order"/> </set> </class>

Everything is ready and the Code for Cascade starts:

Because the code is heavily used to get connections and close connection operations, I use the template design mode, which is explained in my article Hibernate-template mode.

/*** Cascade storage: * 1. inverse = "true", maintain the relationship. It is best to maintain only one party, and maintain only one foreign key. * 2. cascade = "save-update", cascade storage and cascade update */@ Test public void test0 () {new SessionTemplate () {@ Override public void fun (Session s) {Customer c = new Customer (); c. setName ("King"); Order o1 = new Order (); o1.setName ("fried noodles"); Order o2 = new Order (); o2.setName ("meat "); o1.setCustomer (c); // o2.setCustomer (c); s. save (c); s. save (o1); s. save (o2); // This framework only executes three SQL statements} cmd.exe cute ();}
/*** Cascading deletion: * 1. inverse = "false" is the default processing method: Change the foreign key of the customer's order to null, and then delete the customer * 2. inverse = "true" if the customer does not handle this issue, an error will occur */@ Test public void test1 () {new SessionTemplate () {@ Override public void fun (Session s ){
// This is because cascade deletion is not used. In this case, cascade is save-update Customer c = (Customer) s. get (Customer. class, 3); for (Order o: c. getOrders () {s. delete (o);} s. delete (c); // at this time, inverse = "true", so the order of all customers is deleted before deleting the customer

// Modify the configuration file. cascade is delete, and cascade deletion is performed.
Customer c = (Customer) s. get (Customer. class, 3); s. delete (c); // you do not need to manually delete the order.
            }        }.execute();    }
/*** Cascade update
* Cascade: save-update */@ Test public void test2 () {new SessionTemplate () {@ Override public void fun (Session s) {Customer c = (Customer) s. get (Customer. class, 2); for (Order o: c. getOrders () {o. setName ("goat's soup"); // persistent state, no need to update} cmd.exe cute ();}

The query involves the loading policy, so it is very complicated and will be written in the next article.

  • Next we will discuss the situation of student-orientation. We should first set up a scenario: the relationship between students and courses.
Public class Student {private Integer id; private String name; private Set <Course> courses = new HashSet <> (); // many-to-many} public class Course {private Integer id; private String name; private Set <Student> students = new HashSet <> (); // many-to-many}

The following is the ing between two object classes and database tables:

// Student. hbm. xml 

Proceed with all preparations and start code work:

/* ** Multiple-to-multiple * cascade storage:
* I wanted to write the parent join deletion and cascade update, but I found that the written code is different from the result I want to implement, so I didn't paste the code... I mean, neither will I... pretty embarrassing... * // @ Test public void test0 () {new SessionTemplate () {@ Override public void fun (Session s) {Student s1 = new Student (); s1.setName ("King "); course c1 = new Course (); c1.setName (""); Course c2 = new Course (); c2.setName ("English"); s1.getCourses (). add (c1); // The Student maintains the s1.getCourses () relationship (). add (c2); // The Student maintains the relationship s. save (s1) ;}cmd.exe cute ();}

Add, delete, modify, and query. Now, the "query" is missing. I will write Hibernate's query loading policy in the next summary.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.