The mapping of multiple to multiple manytomany of the three-table problem of Hibernate is 3-table annotation version-corresponds to (17) XML version (23)

Source: Internet
Author: User

1

Package com.bjsxt.hibernate;
Import Java.util.HashSet;

Import Java.util.Set;
Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
Import Javax.persistence.JoinColumn;
Import javax.persistence.JoinTable;

Import Javax.persistence.ManyToMany;
	@Entity public class Student {private int id;
	private String name;
	
	Private set<course> courses = new hashset<course> ();
		@ManyToMany @JoinTable (name= "Score", joincolumns= @JoinColumn (name= "student_id", referencedcolumnname= "id"), inversejoincolumns= @JoinColumn (name= "course_id", referencedcolumnname= "id")) public set<course> getcourses ()
	{return courses;
	public void setcourses (set<course> courses) {this.courses = courses;
	@Id @GeneratedValue public int getId () {return Id;
	The public void setId (int id) {this.id = ID;
	Public String GetName () {return name;
	public void SetName (String name) {this.name = name;
 }
	
}

2

Package com.bjsxt.hibernate;

Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;
Import Javax.persistence.JoinColumn;
Import Javax.persistence.ManyToOne;
Import javax.persistence.Table;

@Entity
@Table (name= "score") public
class score {
	private int id;
	private int score;
	Private Student Student;
	Private Course Course;
	@Id
	@GeneratedValue Public
	int getId () {return
		Id;
	}
	public void setId (int id) {
		this.id = ID;
	}
	public int Getscore () {return
		score;
	}
	public void SetScore (int score) {
		this.score = score;
	}
	@ManyToOne
	@JoinColumn (name= "student_id") public
	Student Getstudent () {return
		student;
	}
	public void Setstudent (Student Student) {
		this.student = Student;
	}
	
	@ManyToOne
	@JoinColumn (name= "course_id") public
	Course GetCourse () {return
		course;
	}
	public void Setcourse (Course Course) {
		this.course = Course;
	}
	
}

3

Package com.bjsxt.hibernate;

Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.Id;

@Entity public
class Course {
	private int id;
	private String name;
	@Id
	@GeneratedValue 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;
	}
	
}


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.