-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Session Cache not tracking collection objects?
PostPosted: Tue May 31, 2005 5:04 pm 
Newbie

Joined: Mon Apr 11, 2005 1:14 pm
Posts: 7
Location: Charlotte, NC
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 Functionality
Code:
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;
}



Top
 Profile  
 
 Post subject:
PostPosted: Tue May 31, 2005 5:53 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
You should read 7.3.2. Bidirectional associations

I believe that If you have bidirectional association between two entities, you ineed to manage both ends in your java objects.



Your EmployeeRole.setEmployee() should do:

public void setEmployee(Employee empl)
{
this.employee = empl;
empl.getRoles().add(this);
}

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Wed Jun 01, 2005 1:06 pm 
Newbie

Joined: Mon Apr 11, 2005 1:14 pm
Posts: 7
Location: Charlotte, NC
I must say, that was a refreshing and useful response. After performing this change it works great. Thanks, vgiguere.

... a nice change to the pompus and useless garbage that mostly comes out of this forum.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 02, 2005 5:37 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
hehehe..

Glad I could help.

Good luck.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 02, 2005 5:41 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
Oh, and by the way.

If this is useless or painful for you to manage the association on both end, you can set one end with -inverse=true- Hibernate will just ignore that side of the relationship when persisting changes.

I am not 100% sure, but I believe that this is what it does.

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.