One to many and many to many relationships of 142 objects in Java Foundation

Keywords: Java

After learning the set framework, we can use sets to represent one to many relationships and many to many relationships of objects in actual development.


The teacher is one
Many students
So write like this

It means that you can visit many students in a teacher class

Write only one teacher in the student class

Second, define the students and teachers, then associate them with each other, put the students into the teacher entity class, and bind these students to the teacher t1

Then output

It is generally not recommended to express many to many relationship directly
It's usually split into two one to many
Now show the relationship between students and courses
Add an intermediate object

java entity class code is not written out
Write the Student set and Course set in StudentAndCourse to represent multiple objects write a StudentAndCourse object in the Student and Course classes respectively
Such expression

package com.vince;

/**
 * One to many relationships of objects
 * 
 * @author vince
 * @description
 */
public class OneToManyDemo {

	public static void main(String[] args) {

		Teacher t1 = new Teacher("Mr. Zhang",18,"female");
		Student s1 = new Student("petty thief",10);
		Student s2 = new Student("Xiao Wang",12);
		Student s3 = new Student("Xiao Zhao",11);
		
		//Correlation
		t1.getStudents().add(s1);
		t1.getStudents().add(s2);
		t1.getStudents().add(s3);
		
		s1.setTeacher(t1);
		s2.setTeacher(t1);
		s3.setTeacher(t1);
		
		print(t1);
	}

	private static void print(Teacher t1) {
		System.out.println(t1.getName());
		for(Student s: t1.getStudents()){
			System.out.println(s);
		}
	}

}

Teacher

package com.vince;

import java.util.HashSet;

//One teacher corresponds to many students
public class Teacher {

	private String name;
	private int age;
	private String sex;
	private HashSet<Student> students = new HashSet<>();
	
	
	public HashSet<Student> getStudents() {
		return students;
	}
	public void setStudents(HashSet<Student> students) {
		this.students = students;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "Teacher [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	public Teacher() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Teacher(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	
}

Student s

package com.vince;

/**
 * 
 * @author vince
 * @description
 */
public class Student {

	private int sid;
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	private String sname;
	private int age;
	private Teacher teacher;
	
	
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Student(String sname, int age) {
		super();
		this.sname = sname;
		this.age = age;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Student [sname=" + sname + ", age=" + age + "]";
	}
	
}

Course

package com.vince;

public class Course {

	private int cid;
	private String cname;
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	
}

StudentAndCourse intermediate object class

package com.vince;

public class StudentAndCourse {

	private int id;
	private int sid;
	private int cid;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	
	
}

Posted by matthew1 on Sun, 24 Nov 2019 13:31:09 -0800