I started to play around with Hibernate since yesterday and came with the following example of OneToMany relationship example, but I am not sure if I am doing right and I have no one around me knowing Hibernate, so I post the code here and if you have time please take a quick look of it, then maybe point out if I am wrong in some points. Thanks in advance.
My goal is to use One-to-Many relationship with annotations to perform basic Insert and Update
The Entity classes are from http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-many-using-annotations-1.html
Student
@Entity
@Table(name = "STUDENT")
public class Student {
private long studentId;
private String studentName;
private Set<Phone> studentPhoneNumbers = new HashSet<Phone>(0);
public Student() {
}
public Student(String studentName, Set<Phone> studentPhoneNumbers) {
this.studentName = studentName;
this.studentPhoneNumbers = studentPhoneNumbers;
}
@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
public long getStudentId() {
return this.studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
@Column(name = "STUDENT_NAME", nullable = false, length = 100)
public String getStudentName() {
return this.studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "STUDENT_PHONE", joinColumns = { @JoinColumn(name = "STUDENT_ID") }, inverseJoinColumns = { @JoinColumn(name = "PHONE_ID") })
public Set<Phone> getStudentPhoneNumbers() {
return this.studentPhoneNumbers;
}
public void setStudentPhoneNumbers(Set<Phone> studentPhoneNumbers) {
this.studentPhoneNumbers = studentPhoneNumbers;
}
//I added in this method for updating
public void addPhone(Phone phone) {
this.studentPhoneNumbers.add(phone);
}
}
Phone
@Entity
@Table(name = "PHONE")
public class Phone {
private long phoneId;
private String phoneType;
private String phoneNumber;
public Phone() {
}
public Phone(String phoneType, String phoneNumber) {
this.phoneType = phoneType;
this.phoneNumber = phoneNumber;
}
@Id
@GeneratedValue
@Column(name = "PHONE_ID")
public long getPhoneId() {
return this.phoneId;
}
public void setPhoneId(long phoneId) {
this.phoneId = phoneId;
}
@Column(name = "PHONE_TYPE", nullable = false, length=10)
public String getPhoneType() {
return this.phoneType;
}
public void setPhoneType(String phoneType) {
this.phoneType = phoneType;
}
@Column(name = "PHONE_NUMBER", nullable = false, length=15)
public String getPhoneNumber() {
return this.phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
CreateMain: create the entry in STUDENT, STUDENT_PHONE and PHONE tables
public class CreateMain {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Set<Phone> phoneNumbers = new HashSet<Phone>();
phoneNumbers.add(new Phone("house", "32354353"));
phoneNumbers.add(new Phone("mobile", "9889343423"));
Student student = new Student("Eswar", phoneNumbers);
session.save(student);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}
UpdateMain: Insert a new PHONE entry for the entry created in CreateMain
public class UpdateMain {
public static void main(String[] args) {
String testupd = "Eswar";
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Query queryResult = session.createQuery("from Student");
List allStudents = queryResult.list();
for (int i = 0; i < allStudents.size(); i++) {
Student student = (Student) allStudents.get(i);
if(student.getStudentName().compareTo(testupd)==0) {
student.addPhone(new Phone("test","12345678"));
session.update(student);
}
}
session.getTransaction().commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}
DeleteMain: delete the entry
public class UpdateMain {
public static void main(String[] args) {
String testupd = "Eswar";
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Query queryResult = session.createQuery("from Student");
List allStudents = queryResult.list();
for (int i = 0; i < allStudents.size(); i++) {
Student student = (Student) allStudents.get(i);
if(student.getStudentName().compareTo(testupd)==0) {
student.addPhone(new Phone("test","12345678"));
session.update(student);
}
}
session.getTransaction().commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}