LAZY (unlike EAGER) is considered in JPA as a hint and in some cases data is fetched eagerly regardless of the hint. This happens, for example, when ObjectDB uses reflection mode (classes are not enhanced).
In your example, the lazy location collection is loaded eagerly even when the classes are enhanced because of your implementation of the setLocationList method, which iterates over the collection:
public void setLocationList(List<Location> locationList) {
for(Location location : locationList) {
location.setOwningObject(this);
}
this.locationList = locationList;
}
This method is used by ObjectDB to initialize the field with an empty lazy proxy collection (because your application uses JPA property access rather than field access), but then the application forces ObjectDB to load the collection content.
If you change the method to:
public void setLocationList(List<Location> locationList) {
this.locationList = locationList;
}
and enhance the classes - the test will throw NullPointerException as expected because of an attempt to use the unloaded location collection after closing the EntityManager.