You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by Craig L Russell <cr...@oracle.com> on 2010/06/03 13:30:35 UTC

Loading toOne references

I had an interesting conversation at Jazoon with a fellow who has a  
simple problem that he can't seem to get to work in OpenJPA without  
using implementation specific APIs.

He has a Department and Employees in a OneToMany and ManyToOne  
relationship.

He has the Employee.department field lazily loaded because it's just  
not needed in most application usages.

The application code wants to get the Department.id after querying  
Employees.

The simple employee.getDepartment().getId() doesn't need to go to the  
database, since the department id was loaded from the database when  
the employee was fetched. And yet, OpenJPA goes to the database in  
order to instantiate all the values in the department object, even  
though all the data that the application needs (id) is already in  
memory.

This case was one of the big reasons we implemented the hollow state  
of the object, so the id would always be available. The  
employee.getDepartment() should simply create a hollow Department  
based on the id and await further action by the user. If all the user  
needs is the id, it's available. If any other field is referenced,  
then OpenJPA should go to the database and fetch the load fetch group  
of the field that's fetched. If OpenJPA eagerly loads the department  
based on the getId method, it defeats the purpose of the load fetch  
group for the field that's accessed (after the id field).

I haven't checked to see whether this behavior is simply a fantasy of  
mine or whether the behavior changed over time.

Craig

Craig L Russell
Architect, Oracle
http://db.apache.org/jdo
408 276-5638 mailto:Craig.Russell@oracle.com
P.S. A good JDO? O, Gasp!