Problem Definition:
Inside the same session, it seems that when I persist a collection object outside of it's parent, the parent object will not include it in a subsequent request.
However, if another session is opened then the additional object is retrieved.
Is this a known limitation of the session cache?
Hibernate version: 3
Name and version of the database you are using: Oracle 9i
Problem:
Assertion Failure:
Two Roles Should exist On Employee expected:<2> but was:<1>
Unit Test Output
*** Setting up: testCollectionsWithStandardObject ***
Create a new employee with a Role:
Hibernate: select ENTITY_ID_SEQ.nextval from dual
Hibernate: select ENTITY_ID_SEQ.nextval from dual
Hibernate: insert into Employee (VERSION, CORPORATE_ID, ...) values (?, ?, ...)
Hibernate: insert into EMPLOYEE_ROLE (VERSION, EMPLOYEE_ID,...) values (?, ?, ?, ?)
Create a new Role with employee assigned & persist the Role:
Hibernate: select ENTITY_ID_SEQ.nextval from dual
Hibernate: insert into EMPLOYEE_ROLE (VERSION, EMPLOYEE_ID, ...) values (?, ?, ?, ?)
Two Roles should exist, actual count: 1
Get New Session:
Hibernate: select employee0_.EMPLOYEE_ID ... from Employee employee0_ where employee0_.EMPLOYEE_ID=?
Hibernate: select roles0_.EMPLOYEE_ID ... from EMPLOYEE_ROLE roles0_ where roles0_.EMPLOYEE_ID=?
Two Roles should exist, actual count: 2
*** Shutting down: testCollectionsWithStandardObject ***
Unit Test
Code:
public void testCollectionsWithStandardObject() throws Exception {
Delegate delegate = new Delegate();
System.out.println("Create a new employee with a Role");
Employee employee = new Employee();
employee.setCorporateId("A12345");
employee.setFirstName("test");
employee.setLastName("test");
Set roles = new HashSet();
EmployeeRole role = new EmployeeRole();
role.setRole("Blah");
role.setEmployee(employee);
roles.add(role);
employee.setRoles(roles);
Long employeeId = delegate.persistEmployee(employee);
employee = delegate.getEmployee(employeeId);
assertEquals("One Role Should exist On Employee",1, employee.getRoles().size());
System.out.println("Create a new Role with employee assigned & persist the Role");
EmployeeRole a = new EmployeeRole();
a.setEmployee(employee);
a.setRole("Boom");
Long id = delegate.persistEmployeeRole(a);
employee = delegate.getEmployee(employeeId);
System.out.println("Two Roles should exist, actual count: " + employee.getRoles().size());
// assertEquals("Two Roles Should exist On Employee",2, employee.getRoles().size());
System.out.println("Get New Session:");
Delegate delegate2 = new Delegate();
employee = delegate2.getEmployee(employeeId);
System.out.println("Two Roles should exist, actual count: " + employee.getRoles().size());
assertEquals("Two Roles Should exist On Employee",2, employee.getRoles().size());
}
Persist Object FunctionalityCode:
protected Long persistObject(SkeletonValueObject obj) {
Long tempId = null;
Transaction tx = session.beginTransaction();
tempId = obj.getId();
if (tempId != null) {
session.update(obj);
}
else {
Long tempLong = (Long) session.save(obj);
tempId = tempLong;
}
tx.commit();
return tempId;
}