TestOne-to-many relationship (class-student)
Class construction method (Wihtout ID and students)
Student's construction method (without ID) and non-parametric construction method
Add the corresponding class number to the student table, you need to add students to the class, establish an association relationship
Package Entity;import Java.util.set;import Org.hibernate.session;import org.hibernate.transaction;import Util.hibernateutil;public class Test {public static void main (string[] args) {//add (); Findstudentbygrade (); Update (); Delete (); }//Add students to class public static void Add () {Grade g = new Grade ("One Class electrical", "Information Academy"); Student stu1 = new Student ("Still", "female"); Student STU2 = new Student ("Pastoral Month", "female"); To add the corresponding class number to the student table, you need to add students to the class and establish an association relationship g.getstudents (). Add (STU1); G.getstudents (). Add (STU2); Session session = Hibernateutil.getsession (); Transaction tx = Session.begintransaction (); Session.save (g); Session.save (STU1); Session.save (STU2); Tx.commit (); Hibernateutil.closesession (session); }//Student Information inquiry, Class middle School students ' information//establish association relationship, you can easily navigate from one object to another//note the direction of the Association class---> Student public static void Findstudentbygrade () {Session session = Hibernateutil.getsession (); Grade Grade = (Grade) session.get (grade.class,1); System.out.println (Grade.getgname () + "," +grade.getgdesc ()); set<student> students = grade.getstudents (); for (Student stu:students) {System.out.println (Stu.getsname () + "," +stu.getsex ()); }}//modify student information public static void update () {Grade g = new Grade ("Hiking Class", "Hiking Academy"); Session session = Hibernateutil.getsession (); Transaction tx = Session.begintransaction (); Student stu = (Student) session.get (Student.class, 1); G.getstudents (). Add (Stu); Session.save (g); Tx.commit (); Hibernateutil.closesession (session); }//Delete student information public static void Delete () {Session session = Hibernateutil.getsession (); Transaction tx = Session.begintransaction (); Student stu = (Student) session.get (Student.class, 1); Session.delete (Stu); Tx.commit (); Hibernateutil.closesession (session); }}
One-way many-to-one associationBy using a reference in the multi-party holder, you need to use the
- Define a multi-party reference in a multi-party persistence class, and generate Getter/setter
Configuration of the mapping file (multi-party);
Package test;
Import org.hibernate.Session;
Import org.hibernate.Transaction;
Import Util.hibernateutil;
Import entity. Grade;
Import entity. Student;
Test one-way many-to-one (student-Class)
public class Test2 {
public static void Main (string[] args) {
Save ();
}
//保存public static void save(){ Grade g = new Grade("电气一班", "信息学院"); Student stu1 = new Student("王九九","女"); Student stu2 = new Student("林浣溪", "女"); //设置关联关系 stu1.setGrade(g); stu2.setGrade(g); Session session = new hibernateUtil().getSession(); Transaction tx = session.beginTransaction(); session.save(g); session.save(stu1); session.save(stu2); tx.commit(); hibernateUtil.closeSession(session);}
}
Bidirectional many-to-onepackage test;import org.hibernate.Session;import org.hibernate.Transaction;import util.hibernateUtil;import entity.Grade;import entity.Student;//测试单向多对一(学生--->班级)public class Test2 { public static void main(String[] args) { save(); } //保存 public static void save() { Grade g = new Grade("电气一班", "信息学院"); Student stu1 = new Student("王九九","女"); Student stu2 = new Student("林浣溪", "女"); //设置关联关系 g.getStudents().add(stu1); g.getStudents().add(stu2); stu1.setGrade(g); stu2.setGrade(g); Session session = new hibernateUtil().getSession(); Transaction tx = session.beginTransaction(); session.save(g); session.save(stu1); session.save(stu2); tx.commit(); hibernateUtil.closeSession(session); }}
Inverse propertySet Label
Specifies the control direction of the association relationship, which is maintained by one side by default
In an association relationship, the inverse= "false" is the active party, which is responsible for maintaining the association relationship
In a one-to-many association, only one side of inverse is set to true, which can help improve performance
Cascade Propertysession.save(g);session.save(stu1);session.save(stu2);
- Operation Trouble, save class, still need to save student's operation
- Cascade Operation Simplification
Hibernate automatically persists the associated object when the Cascade property is not set to none
The setting of the Cascade property will result in performance changes and should be carefully set
Query operationsFind information about your student's class
//查询学生所在班级信息public static void findGradeByStudent(){ Session session = hibernateUtil.getSession(); Student stu =(Student) session.get(Student.class, 2); System.out.println(stu.getSid()+","+stu.getSname()+","+stu.getSex()); Grade g = stu.getGrade(); System.out.println(g.getGid()+","+g.getGname()+","+g.getGdesc()); hibernateUtil.closeSession(session);}
SummarizeImplement one-to-many
- Add a collection that holds the many party in the entity of one side
- Add One-to-many tags to the one-side configuration file
Implement one-way many-to-one
- Add one-side references to entities in the many party
- Add Many-to-one tags to the many side's configuration file
Common Properties
- Cascade: Setting cascading relationships
- Inverse: Set which party maintains the affinity relationship
MyEclipse Tips for use
Use the support provided by the IDE to simplify operations
Add a database connection viewupper right corner of MyEclipse-open perspective-myeclipse Database Explorer
or a
Menu bar-window-show view-other expand myeclipse Database Select DB Browser
Reverse engineering
Automatic generation of persisted classes and mapping relationship files based on tables in the database
To create a physical package entity
Resources
- IMOOC Hibernate one of the multiple mappings
- Code
Hibernate---One-to-many association mappings