Skip to content

Commit

Permalink
evolve(hibernate): @manytomany create demo
Browse files Browse the repository at this point in the history
  • Loading branch information
chengcyber committed Aug 5, 2018
1 parent 1b3cd4e commit a0f5688
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.luv2code.hibernate.demo;

import com.luv2code.hibernate.demo.entity.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class CreateCourseAndStudentsDemo {

public static void main(String[] args) {

// create session factory
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Instructor.class)
.addAnnotatedClass(InstructorDetail.class)
.addAnnotatedClass(Course.class)
.addAnnotatedClass(Review.class)
.addAnnotatedClass(Student.class)
.buildSessionFactory();

// create session
Session session = factory.getCurrentSession();

try {
// start transaction
session.beginTransaction();

// create a course
Course course = new Course("Pacman - How to Scorce One Million Points");


// save the course ... and leverage the cascade all :-)
System.out.println("Saving the course");
System.out.println(course);
session.save(course);
System.out.println("Saved the course.");

// create students
Student stu1 = new Student("John", "Doe", "john@gmail.com");
Student stu2 = new Student("Mary", "Public", "mary@gmail.com");

// add students to course
course.addStduent(stu1);
course.addStduent(stu2);

// save the students
System.out.println("Saving students...");
System.out.println(stu1);
System.out.println(stu2);
session.save(stu1);
session.save(stu2);
System.out.println("Saved students: " + course.getStudents());

// commit
session.getTransaction().commit();

System.out.println("DONE!");

} catch (Exception e) {
e.printStackTrace();
} finally {

// add clean up code
session.close();
factory.close();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public class Course {
cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
@JoinTable(
name="course_student",
joinColumns=@JoinColumn("course_id"),
inverseJoinColmns=@JoinColumn("stduent_id")
joinColumns=@JoinColumn(name="course_id"),
inverseJoinColumns=@JoinColumn(name="student_id")
)
private List<Student> students;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class Student {
cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
@JoinTable(
name="course_student",
joinColumns=@JoinColumn("student_id"),
inverseJoinColmns=@JoinColumn("course_id")
joinColumns=@JoinColumn(name="student_id"),
inverseJoinColumns=@JoinColumn(name="course_id")
)
private List<Course> courses;

Expand Down

0 comments on commit a0f5688

Please sign in to comment.