Hi! I am currently following the tutorial in the documentation and trying out saving a collection of values. The problem I have is that my application is saving collection of entities(Set<Course> courses) but not the collection of values(Set<String> contactDetails).
Here is my persistent class..
Code:
public class Student {
private Long id;
private String firstName;
private String lastName;
private Set<Course> courses = new HashSet<Course>();
private Set<String> contactDetails = new HashSet<String>();
//getters and setters
}
Here is my mapping file
Code:
<hibernate-mapping package="com.phoenixone.school.model">
<class name="Student" table="student">
<id name="id" column="studentId">
<generator class="native" />
</id>
<property name="firstName" />
<property name="lastName" />
<set name="courses" table="student_course">
<key column="studentId" />
<many-to-many column="courseId" class="Course" />
</set>
<set name="contactDetails" table="contactDetails">
<key column="studentId" />
<element type="string" column="contactDetail" />
</set>
</class>
</hibernate-mapping>
Here is the part of my DAO where I save the persistent class...
Code:
public class StudentDaoHibernate {
public void save(Student student){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
}
}
Here is my test...
Code:
public class TestStudentDaoHibernate {
public static void main(String[] args) {
CourseDaoHibernate courseDao = new CourseDaoHibernate();
Course c1 = new Course();
c1.setCourseCode("CWD");
c1.setCourseName("Web Dev");
courseDao.save(c1);
Set<Course> courses = new HashSet<Course>();
courses.add(c1);
Student student = new Student();
student.setFirstName("Bob");
student.setLastName("Santos");
student.setCourses(courses);
student.getContactDetails().add("123456789");
StudentDaoHibernate dao = new StudentDaoHibernate();
dao.save(student);
}
}
I've tried doing the saving of the contactDetails this way but it still does not save the String...
Code:
...
Set<String> contactDetails = new HashSet<String>();
contactDetails.add("123456789");
...
student.setContactDetails(contactDetails);
...
I am not seeing any table which contains the String "123456789" that I tried to save in my test.
I've also read from Java Persistence with Hibernate that the table which contains these values are hidden from me. Is my understanding correct?
Am I missing something or just doing the saving of collection of values wrong?
Thanks in advance!