Hibernate (eight) __ Cascade operation, struts+hibernate+ Interface Programming Architecture

Source: Internet
Author: User
Tags ming

Cascade Operations

the So-called cascade operation means that when you perform an operation on the main object, the corresponding action is done automatically from the object Hibernate.

For example : Department <---->student Object relationship, I hope that when I delete a Department, It will automatically delete all the students in that Department.

Another example : BBS project main post <----> replies , the main post deleted, then we would like to the main post of the replies automatically deleted, so that we can use the Cascade (cascade) Operation.

Case : How to configure cascading actions, we automatically delete students when a department is deleted .

first, We are modifications in the configuration file :

<!-- Configure one-to-many relationship cascade= "delete" When the department is deleted ( main object, Then cascade delete it from the student from the object)--

<set name= "stus" cascade= "delete">

<!-- Specify the foreign key for the Student class--

<key column= "dept_id"/>

<one-to-many class= "Student"/>

</set>

operation in Java code :

Department department=new Department ();

Department.setname (" Business unit ");

Student stu1=new Student ();

stu1.setname ("little Red ") ;

Stu1.setdept (department);

Student stu2=new Student ();

stu2.setname (" Xiao Ming ");

Stu2.setdept (department);

Session.save (department);

Session.save (stu1);

Session.save (stu2);

Department department= (Department) s.get (department.class, 41);

S.delete (department);

Save-update

configuration file :

<set name= "stus" cascade= "save-update" >

<!-- Specify the foreign key for the Student class--

<key column= "dept_id"/>

<one-to-many class= "Student"/>

</set>

Java code:

1.Department department=new Department ();

Department.setname ("business unit");

Student stu1=new Student ();

Stu1.setname ("little red");

Stu1.setdept (department);

Student stu2=new Student ();

Stu2.setname ("xiao ming");

Stu2.setdept (department);

Session.save (department);

Although cascading operations are set up, in this case only the department is saved because there is no reverse foreign key reference set from the department to the Student.

however, If you set up the cascade operation on the student side (students are referenced by the foreign key of the department), you can save the students when they are Saved.

When you are still doing the operation from the department side, you should change to the following actions:

2.Department department=new Department ();

Department.setname (" Business unit 3");

Student stu1=new Student ();

Stu1.setname ("little Red ");

Student stu2=new Student ();

Stu2.setname (" Xiao Ming ");

set<student> students=new hashset<student> ();

Students.add (stu1);

Students.add (stu2);

Department.setstus (students); Explicitly save with collection relationships

S.save (department);

Description

① can be used in both collection properties and normal properties Cascade

② Generally speaking cascade configuration in One-to-many (one side, such as employee-department), and one-to-one (main object Side)

Struts+hibernate+ Interface Programming

Case: Message Book

This is a picture of a frame in the hibernate case that Hanshunping teacher Said. From the top down are the web layer, business layer, DAO layer, hibernate layer, Database Layer.

The key to learning in this area is the way the interface is programmed in this framework, focusing on the design structure .

The interface in the framework enables the Web layer and the business layer to Decouple. The method is declared in the interface, its methods are implemented in the business layer, but the methods implemented in the business layer may need to be changed,

In order to keep the code invariant in the Web layer , we use the interface invocation method at the Web layer,

Usersserviceinter usersserviceinter=new Usersserviceimp ();

And after we learn to use Spring , we can configure Usersserviceimp () in the way that the XML file is configured , which is more decoupled .

There is also the definition of the underlying interface and the underlying implementation class. Because there may be public methods in Messageserviceinter and userserviceinter, in order to enhance the reusability of the code, add a

The underlying interface, and only the Messageserviceinter and Userserviceinter define their own unique METHODS. At the same time Baseserviceimpl is an abstract class , it can not fully implement

Methods in baseserviceinter, because some methods require their successors to be unique implementations without defining a uniform method implementation .

Hibernate (eight) __ Cascade operation, struts+hibernate+ Interface Programming Architecture

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.