Hibernate one-To-many bi-directional:
Instructor class:
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Instructor {
private int id;
private String first_name;
private String last_name;
private String email;
private List<Course> courses;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@OneToMany(mappedBy = "instructor",cascade = CascadeType.ALL)
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
//convenient method for bi-directional relationship.
public void add(Course tempCourse) {
if(courses==null) {
courses = new ArrayList<Course>();
}
courses.add(tempCourse);
tempCourse.setInstructor(this);
}
}
Course class:
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Course {
private int id;
private String title;
private Instructor instructor;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "instructor_id")
public Instructor getInstructor() {
return instructor;
}
public void setInstructor(Instructor instructor) {
this.instructor = instructor;
}
@Override
public String toString() {
return "Course [id=" + id + ", title=" + title + ", instructor=" + instructor + "]";
}
}
Main class:
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class MainApp {
public static void main(String[] args) {
SessionFactory sessionFactory = (SessionFactory) new Configuration().configure("Hibernate.cfg.xml")
.addAnnotatedClass(Instructor.class).addAnnotatedClass(Course.class).buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
try {
Transaction tx = session.beginTransaction();
Instructor i1 = new Instructor();
i1.setFirst_name("nishat");
i1.setLast_name("nabila");
i1.setEmail("nabila@gmail.com");
Course c1 = new Course();
c1.setTitle("math");
c1.setInstructor(i1);
Course c2 = new Course();
c2.setTitle("physics");
c2.setInstructor(i1);
Course c3 = new Course();
c3.setTitle("social science");
c3.setInstructor(i1);
List<Course> allCourse = new ArrayList<Course>();
allCourse.add(c1);
allCourse.add(c2);
allCourse.add(c3);
i1.setCourses(allCourse);
session.save(i1);
tx.commit();
}
finally {
session.close();
}
}
}