Please enable JavaScript.
Coggle requires JavaScript to display documents.
Mapping, OneToOne: mỗi đối tượng của lớp A tương ứng với một đối tượng của…
-
OneToOne: mỗi đối tượng của lớp A tương ứng với một đối tượng của lớp B và ngược lại
có thể là đối ngẫu hoặc hai chiều
cbi database table
trong bảng instructor:
instructor_detail_id int DEFAULT NULL,
CONSTRAINT FK_DETAIL FOREIGN KEY (instructor_detail_id) REFERENCES instructor_detail (id)
tạo lớp InstructorDetail Entity
tạo lớp Instructor Entity
OneToOne(cascade = CascadeType.ALL)
JoinColumn(name = "instructor_detail_id")
private InstructorDetail instructorDetail;
public Instructor(String first_name, String last_name, String email) {
this.first_name = first_name;
this.last_name = last_name;
this.email = email;}
-
tạo main app
try {
Instructor tempInstructor = new Instructor("Chad", "Darby", "darby@luv2code.com");
InstructorDetail tempInstructorDetail = new InstructorDetail("youtube", "code"); tempInstructor.setInstructorDetail(tempInstructorDetail);
delete
int theId = 1;
session.beginTransaction();
Instructor tempInstructor = session.get(Instructor.class, theId);
if (tempInstructor != null) {
session.delete(tempInstructor);}
session.getTransaction().commit();
update InstructorDetail
Add field to refer Instructor
OneToOne(mappedBy = "instructorDetail", cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
private Instructor instructor;
-
-
-
create main app
int theId = 2;
InstructorDetail tempInstructorDetail = session.get(InstructorDetail.class, theId);
-
khóa ngoại
1 trường trong 1 bảng, tham chiếu tới khóa chính trong bảng khác
OneToMany: mỗi đối tượng của lớp A tương ứng với nhiều đối tượng của lớp B và ngược lại
có thể là đối ngẫu hoặc hai chiều
tạo lớp Course Entity
// nhiều course -> 1 instructor
ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
JoinColumn(name = "instructor_id")
private Instructor instructor;
tạo main app
// get an instructor from db
int theId = 1;
Instructor tempInstructor = session.get(Instructor.class, theId);
// create some courses
Course tempCourse1 = new Course("Air Guitar");
// add courses to the instructor
tempInstructor.add(tempCourse1);
// save the courses
session.save(tempCourse1);
delete
int theId = 10;
session.beginTransaction();
Course tempCourse = session.get(Course.class, theId);
session.delete(tempCourse);
tạo lớp Instructor Entity
// 1 instructor -> nhiều course
OneToMany(mappedBy = "instructor", cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
private List<Course>courses;
-
public void add(Course tempCourse) {
if (courses == null) {
courses = new ArrayList<>();} courses.add(tempCourse);
tempCourse.setInstructor(this);
cbi database table
trong bảng course:
instructor_id int DEFAULT NULL,
CONSTRAINT FK_INSTRUCTOR FOREIGN KEY (instructor_id) REFERENCES instructor (id)
-
tạo lớp Course Entity
// 1 course -> nhiều review
OneToMany(fetch = FetchType.LAZY,
cascade = CascadeType.ALL)
JoinColumn(name = "course_id")
private List<Review>reviews;
public void add(Review tempReview) {
if (reviews == null) {
reviews = new ArrayList<>();} reviews.add(tempCourse);
tạo main app
Course tempCourse = new Course("CS GO");
tempCourse.addReview(new Review("Love it"));
tempCourse.addReview(new Review("Cool"));
session.save(tempCourse);
-
tạo main app
// create an course from db
Course tempCourse = new Course("Pacman");
session.save(tempCourse);
Student tempStudent1 = new Student("John", "Doe", "john@luv2code.com");
Student tempStudent2 = new Student("Mary", "Public", "mary@luv2code.com"); tempCourse.addStudent(tempStudent1); tempCourse.addStudent(tempStudent2);
session.save(tempStudent1);
session.save(tempStudent2);
delete
int theId = 10;
session.beginTransaction();
Course tempCourse = session.get(Course.class, theId);
session.delete(tempCourse);
-
cbi database table
trong bảng course_student:
course_id int NOT NULL,
student_id int NOT NULL,
PRIMARY KEY (course_id,student_id),
CONSTRAINT FK_COURSE_05 FOREIGN KEY (course_id) REFERENCES course (id),
CONSTRAINT FK_STUDENT FOREIGN KEY (student_id) REFERENCES student (id)
Join
- If the join is for a OneToOne or ManyToOne mapping using a foreign key mapping strategy
cột khóa ngoại nằm trong bảng nguồn hoặc nhúng
- If the join is for a unidirectional OneToMany mapping using a foreign key mapping strategy
khóa ngoại nằm trong bảng đích
- If the join is for a ManyToMany mapping or for a OneToOne or bidirectional ManyToOne/OneToMany mapping using a join table
khóa ngoại nằm trong bảng join
- If the join is for an element collection
khóa ngoại nằm trong bảng collection
-
-
-
-