(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