-
1. Re: greedy loading of associations
adamw Apr 28, 2008 7:35 AM (in response to michalbali)Hello,
that's true, all associations (except for the not-owning-side of one-to-one) are now loaded lazily.
Maybe I'll just check the value of the "fetch" attribute of the annotations and load lazily or not. You think that'd be sufficient?
Adam -
2. Re: greedy loading of associations
michalbali Apr 28, 2008 9:27 AM (in response to michalbali)That sounds good to me. If I remember correctly, that's how hibernate does it. By default, it loads associations lazily, but you can override it - per property or per class.
Thanks,
Michal -
3. Re: greedy loading of associations
awhitford Apr 29, 2008 2:16 AM (in response to michalbali)Just to clarify, the default fetchType varies depending on the type of association:
http://java.sun.com/javaee/5/docs/api/javax/persistence/OneToOne.html#fetch() - EAGER
http://java.sun.com/javaee/5/docs/api/javax/persistence/OneToMany.html#fetch() - LAZY
http://java.sun.com/javaee/5/docs/api/javax/persistence/ManyToOne.html#fetch() - EAGER
http://java.sun.com/javaee/5/docs/api/javax/persistence/ManyToMany.html#fetch() - LAZY -
4. Re: greedy loading of associations
michalbali Apr 29, 2008 4:11 AM (in response to michalbali)Yes, that makes sense.
Thanks,
Michal -
5. Re: greedy loading of associations
scottr0829 Feb 13, 2009 6:14 PM (in response to michalbali)Sorry to bring this older thread back to life.
Adam, did you ever get a chance to implement the "checking of the fetch type" into the association load?
I ask because I have a many-to-one relationship that I eagerly load and I keep getting an error on the entity envers finds because the id of that object is null (optional is false too). I trace through the code and see the variables as being fetched lazily. I verified there is a valid id in the db - it just doesn't seem to be fetching the object. -
6. Re: greedy loading of associations
adamw Feb 14, 2009 9:11 AM (in response to michalbali)Hello,
no, eager loading isn't yet implemented, but it shouldn't cause errors like "id is null". Can you maybe post more details or create a testcase and file a JIRA bug?
--
Adam -
7. Re: greedy loading of associations
scottr0829 Feb 20, 2009 3:20 PM (in response to michalbali)Hi Adam,
Thanks for your response and help.
The code for the method is below:@SuppressWarnings("unchecked") public <T extends Request> String getLatestChanges(Class<T> type, T request) { LOG.trace("Starting getLatestChanges..."); StringBuilder changes = new StringBuilder(); if (request == null || request.getId() == null) { throw new IllegalArgumentException("invalid request passed in"); } request = em.find(type, request.getId()); Integer revNumber = (Integer) getVersionsReader().createQuery().forRevisionsOfEntity(type, false, true) .add(VersionsRestrictions.idEq(request.getId())) .addProjection(RevisionProperty.max()) //.add(VersionsRestrictions.eq("revision_type", RevisionType.MOD)) .getSingleResult(); LOG.debug("Going to get revision #"+revNumber); T prevReq = (T) getVersionsReader().createQuery().forEntitiesAtRevision(type, revNumber) .add(VersionsRestrictions.idEq(request.getId())) .getSingleResult(); if (prevReq == null) { LOG.debug("No changes registered."); } else { if (request instanceof ClockRequest) { changes.append(getClockChanges((ClockRequest) request, (ClockRequest) prevReq)); } else if (request instanceof StudioRequest) { changes.append(getStudioChanges((StudioRequest) request, (StudioRequest) prevReq)); } else { changes.append(getEventChanges((EventRequest) request, (EventRequest) prevReq)); } } LOG.trace("Returning from getLatestChanges..."); return changes.toString(); }
When I run this, everything comes back fine from Envers in the prevReq object except for the eventType attribute - it stays null and the debugger says the variable handler is JavassistLazyInitializer (I found this after putting a breakpoint at the if (prevReq == null) and looking at the object). Looking at the Request object, eventType is of type EventType and is supposed to be eagerly fetched, as well as optional=false. Since there is no eventType with id null (it is required), we are getting a
javax.persistence.EntityNotFoundException: Unable to find EventType with id null error.
Any ideas why envers is not pulling back the event_type_id field from the db? I have confirmed it exists in the record. -
8. Re: greedy loading of associations
scottr0829 Feb 20, 2009 4:58 PM (in response to michalbali)I want to add that the eventType attribute in Request has a ManyToOne relationship and has a join column defined. There are never any issues saving and reading the current version, only the versions returned via the envers query.
-
9. Re: greedy loading of associations
adamw Mar 2, 2009 2:13 PM (in response to michalbali)Hello,
I tried to reproduce your issue but can't. I have a many-to-one relationship, I persist two entities and then query for the many side, and read the related reference - and it's read properly. Maybe you can create a testcase and post a JIRA bug? Or am I not reproducing the case correctly?
--
Adam