Hibernate does not save a set of values

Ok, now I used Hibernate in several projects, but I did not learn its subtleties before using it. I started by exploring the codes that used JPA Annotations and are integrated with Spring, and everything worked fine. But now, when I want to teach basic Hibernate to my students, and I come up with an example and using the tutorial Chapter 1, I had a problem with saving a collection of collection values ​​in one constant class.

Here's a permanent class ...

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
}

Mapping File ...

<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" lazy="false">
            <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>

DAO (retaining part)

public class StudentDaoHibernate {
    public void save(Student student){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        session.save(student);
        session.getTransaction().commit();
    }
}

Part of testing ...

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);
    }
}

When I execute these lines of code ...

Session session = HibernateUtil.getSessionFactory().getCurrentSession()
session.beginTransaction();
Student student = (Student)session.createCriteria(Student.class)
             .add(Restrictions.eq("id", new Long(1))).uniqueResult();
System.out.println("First Name: " + student.getFirstName());
System.out.println("Last Name: " + student.getLastName());

System.out.println("Courses enrolled with...");
for(Course c:student.getCourses()){
    System.out.println(c.getCourseName());
}

System.out.println("Contact details...");
for(String s:student.getContactDetails()){
    System.out.println(s);
}

What I get is ...

First Name: Bob
Last Name: Santos
Courses Received from ...
Web Dev
Contact Details ...

, "123456789" . , ? !

+3
3

contactDetails. , , , . - , , Student, , .

cascade = "all, delete-orphan" , , . . Hibernate 4.3.

+5

session.save(student); session.flush();

+1

I would recommend creating the Contract class and using it instead of String. This is the best abstraction, and the proof that Hibernate is dealing with your course class, just says that it will work well with the Contract.

0
source

Source: https://habr.com/ru/post/1716478/


All Articles