Hibernate:在session.saveOrUpdate()之后,POJO中的HashSet对象更改为PersistentSet

时间:2015-01-27 14:28:30

标签: hibernate

我有一个简单的Hibernate 1到M代码。当我保存Employee对象时,在保存之前,Employee POJO中的java.util.HashSet内的子对象在session.saveOrUpdate()之后变为org.hibernate.collection.PersistentSet。

private static void addEmployee() {
    System.out.println("Entering addEmployee **********");
    Session session = HibernateSessionUtil.getSession("test/hibernate.cfg.xml");
    Employee1toM employee1toM = new Employee1toM();
    employee1toM.setEmployeeFirstName("Jitendra");
    employee1toM.setEmployeeLastName("Patil");

    EmployeeAddress1toM employeeAddress1toM = new EmployeeAddress1toM();
    employeeAddress1toM.setEmployeeArea("ABC");
    employeeAddress1toM.setEmployeeCity("DEF");
    employeeAddress1toM.setEmployeeState("IJK");

    employeeAddress1toM.setEmployee1toM(employee1toM);
    employee1toM.getEmployeeAddress1toM().add(employeeAddress1toM);

    System.out.println(employee1toM.getEmployeeAddress1toM().getClass().getName());
    // prints java.util.HashSet

    Transaction txn = session.beginTransaction();
    session.saveOrUpdate(employee1toM);
    txn.commit();

    System.out.println(employee1toM.getEmployeeAddress1toM().getClass().getName());
    // prints org.hibernate.collection.PersistentSet

    System.out.println("Leaving addEmployee **********");

}

OutPut:
Entering addEmployee **********
java.util.HashSet
Hibernate: insert into Employee1toM (EMPLOYEE_FIRSTNAME, EMPLOYEE_LASTNAME) values (?, ?)
Hibernate: insert into EmployeeAddress1toM (EMPLOYEE_AREA, EMPLOYEE_CITY, EMPLOYEE_STATE, EMPLOYEE_ID) values (?, ?, ?, ?)
org.hibernate.collection.PersistentSet
Leaving addEmployee **********

配置和POJO很简单,所以我不会分享它。请帮助我了解如何停止将 HashSet 自动转换为 PersistentSet 类型。

0 个答案:

没有答案