One To Many
One To Many
One To Many
zip
package com.luv2code.hibernate.demo.entity;
@Entity
@Table(name="instructor")
public class Instructor {
// create constructors
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="email")
private String email;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="instructor_detail_id")
private InstructorDetail instructorDetail;
@OneToMany(mappedBy="instructor",
cascade= {CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.DETACH, CascadeType.REFRESH})
private List<Course> courses;
public Instructor() { }
1
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Instructor [id=" + id + ", firstName=" + firstName + ", lastName="
+ lastName + ", email=" + email + ", instructorDetail=" + instructorDetail + "]";
}
if (courses == null) {
courses = new ArrayList<>();
}
courses.add(tempCourse);
tempCourse.setInstructor(this);
}
}
2
Course.java
package com.luv2code.hibernate.demo.entity;
@Entity
@Table(name="course")
public class Course {
// define constructors
// define tostring
// annotate fields
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="title")
private String title;
public Course() { }
@Override
public String toString() {
return "Course [id=" + id + ", title=" + title + "]";
}
3
CreateInstructorDemo.java
package com.luv2code.hibernate.demo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.luv2code.hibernate.demo.entity.Course;
import com.luv2code.hibernate.demo.entity.Instructor;
import com.luv2code.hibernate.demo.entity.InstructorDetail;
// create session
Session session = factory.getCurrentSession();
try {
// create the objects
Instructor tempInstructor =
new Instructor("Susan", "Public",
"[email protected]");
InstructorDetail tempInstructorDetail =
new InstructorDetail(
"http://www.youtube.com", "Video Games");
// start a transaction
session.beginTransaction();
// commit transaction
session.getTransaction().commit();
System.out.println("Done!");
}
finally {
// add clean up code
session.close();
factory.close();
}
}
}
4
CreateCoursesDemo.java
package com.luv2code.hibernate.demo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.luv2code.hibernate.demo.entity.Course;
import com.luv2code.hibernate.demo.entity.Instructor;
import com.luv2code.hibernate.demo.entity.InstructorDetail;
// create session
Session session = factory.getCurrentSession();
try {
// start a transaction
session.beginTransaction();
// commit transaction
session.getTransaction().commit();
System.out.println("Done!");
}
finally {
factory.close();
}
}
}
5
GetInstructorCoursesDemo.java
package com.luv2code.hibernate.demo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.luv2code.hibernate.demo.entity.Course;
import com.luv2code.hibernate.demo.entity.Instructor;
import com.luv2code.hibernate.demo.entity.InstructorDetail;
// create session
Session session = factory.getCurrentSession();
try {
// start a transaction
session.beginTransaction();
// commit transaction
session.getTransaction().commit();
System.out.println("Done!");
}
finally {
6
DeleteCourseDemo.java
package com.luv2code.hibernate.demo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.luv2code.hibernate.demo.entity.Course;
import com.luv2code.hibernate.demo.entity.Instructor;
import com.luv2code.hibernate.demo.entity.InstructorDetail;
// create session
Session session = factory.getCurrentSession();
try {
// start a transaction
session.beginTransaction();
// get a course
int theId = 10;
Course tempCourse = session.get(Course.class, theId);
// delete course
System.out.println("Deleting course: " + tempCourse);
session.delete(tempCourse);
// commit transaction
session.getTransaction().commit();
System.out.println("Done!");
}
finally {
// add clean up code
session.close();
factory.close();
}
}
}
7
8