"SSH Three frames" Hibernate part eighth: operating-many relationship

Source: Internet
Author: User

In hibernate in the-many relationship. It is not usually used. Because when the database query complexity is too high.

What we do here are students and teachers, students can have multiple teachers, teachers can have multiple students.


watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmdgwmduzma==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">


We first set up a student entity class: Student.java

Package Cn.itcast.hibernate.domain;import Java.util.set;public class Student {private int id;private String name; Private set<teacher> teachers;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;} Public set<teacher> Getteachers () {return teachers;} public void Setteachers (set<teacher> teachers) {this.teachers = teachers;}}
We have defined three three properties. Each is an ID, a name, and a set set


And then the teacher. Entity class: Teacher.java

Package Cn.itcast.hibernate.domain;import Java.util.set;public class Teacher {private int id;private String name; Private set<student> students;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;} Public set<student> getstudents () {return students;} public void Setstudents (set<student> students) {this.students = students;}}
We still define three entity classes, ID, name, and a set set


Then, let's look at the mapping file for the Stduent class:

<?xml version= "1.0"?>  <! DOCTYPE hibernate-mapping public       "-//hibernate/hibernate mapping DTD 3.0//en"      "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">  
In this file. We defined: ID self-increment, name attribute. Another <set> tag. The Name property is that property of the set collection in Student.java. Then we define the foreign key in the <key> tag as student_id, and then define a <many-to-many> tag, which specifies a many-to-many relationship.


Below is the mapping file for the Teacher.java class:

<?

XML version= "1.0"?

> <! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

Almost identical to the mapping file for the Stduent class above


Then, we write a test class: Many2many.java

Package Cn.itcast.hibernate;import Java.util.hashset;import Java.util.set;import org.hibernate.session;import Org.hibernate.transaction;import Cn.itcast.hibernate.domain.student;import Cn.itcast.hibernate.domain.Teacher; public class Many2many {public static void main (string[] args) {add (); query (1);} static void Add () {Session s = null; Transaction tx = null;try{//defines a teahcer set set set<teacher> ts = new hashset<teacher> ();// Defines a set set of Student set<student> ss = new hashset<student> ();//Add a teacher 1Teacher t1 = new Teacher (); T1.setname ( "T1 name"); Ts.add (t1);//Add a teacher 2Teacher t2 = new Teacher (); T2.setname ("T2 name"); Ts.add (T2);//Add a student 1Student S1 = new Student (); S1.setname ("S1"); Ss.add (S1);//Add a student 2Student s2 = new Student (); S2.setname ("S2"); Ss.add (S2);// Here is a set property setting of two teacher t1.setstudents (ss); t2.setstudents (ss);/* * This is a set property that sets two student. If the settings above are present at the same time, an exception is thrown.   Since many-to-many relationships have been established on the top * s1.setteachers (TS);   S2.setteachers (TS); */s = Hibernateutil.getsession (); tx = S.begintransaction (); S.save (t1); S.save (T2); S.save (S1); S.save (S2); Tx.commit ();} Finally{if (s!=null) {s.close ();}}} static void query (int id) {Session s = null; Transaction tx = Null;try{s = Hibernateutil.getsession (); tx = S.begintransaction ();//The Teacher object is obtained by the ID query teacher T = ( Teacher) S.get (Teacher.class, id);//The number of students corresponding to the Teacher object is called out System.out.println ("Students:" +t.getstudents (). Size ()); Tx.commit ();} Finally{if (s!=null) {s.close ();}}}}








Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

"SSH Three frames" Hibernate part eighth: operating-many relationship

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.