I had problems today when lazy loading did not work when using the matched collection. I found this wonderful article that seems to fix the problem.
http://justonjava.blogspot.co.uk/2010/09/lazy-one-to-one-and-one-to-many.html
One thing I don't understand is how the workaround using FieldHandled works. Can someone help me figure this out? Below is the code (copied from the example by reference):
@Entity public class Animal implements FieldHandled { private Person owner; private FieldHandler fieldHandler; @OneToOne(fetch = FetchType.LAZY, optional = true, mappedBy = "animal") @LazyToOne(LazyToOneOption.NO_PROXY) public Person getOwner() { if (fieldHandler != null) { return (Person) fieldHandler.readObject(this, "owner", owner); } return owner; } public void setOwner(Person owner) { if (fieldHandler != null) { this.owner = fieldHandler.writeObject(this, "owner", this.owner, owner); return; } this.owner = owner; } public FieldHandler getFieldHandler() { return fieldHandler; } public void setFieldHandler(FieldHandler fieldHandler) { this.fieldHandler = fieldHandler; } }
What am I missing? Perhaps I do not know enough about the sleeping life cycle here? I'm glad to investigate, but can anyone give me some pointers.
Thanks in advance.
EDIT
I made a lot of changes, so many of my entities implemented FieldHandled, but then discovered that some of my tests were unsuccessful. I deflated SQL and got some weird things where SQL was executed in different orders if this interface was implemented only with these methods.
public FieldHandler getFieldHandler() { return fieldHandler; } public void setFieldHandler(FieldHandler fieldHandler) { this.fieldHandler = fieldHandler; }
This led to the tests failing, because when I argued, the situation was not quite in the correct state. This adds my misunderstanding of this FieldHandler variable.
Rnj
source share