Manytoone Cascade Save

Source: Internet
Author: User

(1) Adding a reference to one party (such as Group) in the many party (such as user) adds "private group group;" In the user class, and generates Geter and Seter.
(2) Add annotations on Geter @manytoone, @JoinColumn (name= "GroupId") where groupId represents the foreign key name of the generated table.
(3) Writing test procedures
1) Get configuration, generate Schemaexport, set up table in database (if existing, delete immediately)
2) Create a new group object and multiple user objects, and set the group property of the user object.
3) Get the session, open things, save the user object, the corresponding group object will be automatically saved to the table.

2.one2many Cascade Save
(1) Add a collection (such as set<user>) to one side (e.g. group)
"Note" Be sure to create a collection with new
Private set<user> users=new hashset<user> ();
(2) Generate getter and setter
(3) Add annotations on Getter
@OneToMany (Cascade=cascadetype.all)
@JoinColumn (name= "GroupId")
(4) Writing test procedures
User U1=new user ();
U1.setname ("Zhang San");
User U2=new user ();
U2.setname ("John Doe");
Group G=new Group ();
G.setname ("Administrators group");
G.getusers (). Add (U1);
G.getusers (). Add (U2);
Session.save (g); Save group, save the corresponding users automatically

Manytoone Cascade Save

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.