Cascade for hibernate cascade operations

Source: Internet
Author: User

This is updated for updates. In fact, there is no need to record these records, and we will know about the poor documentation. When the click-through rate is reached, the search engine will add more articles to the newly opened blog.

--------------------------------------------------------------------------------

In hibernate, cascade updates, such as cascade update and delete, are sometimes required. When insert is used, the data of one table can be updated to automatically update the data of another table.
This is Cascade update ··

Example:
First look at these two entities

User, group

Their relationship is manyto one multi-to-one (multiple students in one group)

Package com. sccin. entity;
 
Import java. util. Set;
 
Import javax. Persistence. entity;
Import javax. Persistence. generatedvalue;
Import javax. Persistence. ID;
 
Import javax. Persistence. onetoence;
Import javax. Persistence. Table;
 
 
@ Entity
@ Table (name = "tb_group ")
Public class group {
 
Private int ID;
Private string name;
 
Private set <user> users;
 
@ Onetoworkflow (mappedby = "group ")
Public set <user> getusers (){
Return users;
}
Public void setusers (set <user> Users ){
This. Users = users;
}
@ ID
@ Generatedvalue
Public int GETID (){
Return ID;
}
Public void setid (int id ){
This. ID = ID;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
}

User

--------------------------------------------------------------------------------
Package com. sccin. entity;
 
Import javax. Persistence. cascadetype;
Import javax. Persistence. entity;
Import javax. Persistence. generatedvalue;
Import javax. Persistence. ID;
 
Import javax. Persistence. manytoone;
Import javax. Persistence. Table;
 
 
@ Entity
@ Table (name = "tb_user ")
Public class user {
 
Private int ID;
Private string name;
 
Private group;
 
@ Manytoone
Public group getgroup (){
Return group;
}
Public void setgroup (group Group ){
This. Group = group;
}
@ ID
@ Generatedvalue ()
Public int GETID (){
Return ID;
}
Public void setid (int id ){
This. ID = ID;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
}

If so, by default

We save a student and set the group to which the student belongs. The student can be saved only when there is an entity in the group.

@ Test

@ Test
Public void cascade (){
Session session = hibernateuitl. getsessionfactory (). opensession ();
User u = new user ();
Group G = new group ();
G. setname ("G ");
U. setname ("AA ");
Session. begintransaction ();
Session. Save (g); // This group exists first
U. setgroup (g );
Session. Save (U); // Save the student
Session. gettransaction (). Commit ();
Session. Close ();
}
How can we automatically save the group for students when we set the group to which the students belong?

You only need to set cascade when setting the relationship between the table and the table.

Modify

@ Manytoone (cascade = cascadetype. All)
Public group getgroup (){
Return group;
}
The document says:

2.2.5.4. Use cascading to achieve propagation persistence (transitive persistence)
You may have noticed that the cascade attribute accepts the cascadetype array. the concept of cascade in ejb3 is very similar to the propagation persistence and cascade operations in hibernate, but there are slight differences in semantics, and the supported cascade types are also somewhat different:

Cascadetype. persist: if an object is in a manageable State, or when the persist () function is called, The create operation is triggered.
Cascadetype. Merge: if an object is in a manageable State, or when the Merge () function is called, the cascade Union (merge) operation is triggered.
Cascadetype. Remove: When the delete () function is called, the cascade Delete (remove) operation is triggered.
Cascadetype. Refresh: When the refresh () function is called, the cascade update operation is triggered.
Cascadetype. ALL: all of the above

When cascade is set
@ Test

@ Test
Public void cascade (){
Session session = hibernateuitl. getsessionfactory (). opensession ();
User u = new user ();
Group G = new group ();
G. setname ("G ");
U. setname ("AA ");
U. setgroup (g );
Session. begintransaction ();
// Session. Save (g); you do not need to manually Save the group. The group is automatically saved when the user is saved.
 
Session. Save (U );
Session. gettransaction (). Commit ();
Session. Close ();
}

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.