I am using the Jpa interface.
I have two classes: Parent, Child. Parent contains a "Set<Child> children", and Child contains a "Parent parent". Respective one-to-many and many-to-one relationships are specified in XML.
In a test, I created a Parent (call it testParent1) and a Child (call it testChild1). Insert them into the database via EntityManager. Then I tried to retrieve the data I just inserted via JPQL.
JPQL 1: SELECT p FROM Parent p LEFT JOIN FETCH p.children c
JPQL 1-Result: It returned testParent1 whose Set<Child> is empty. testChild1 can't be found anywhere in result.
JPQL 2: SELECT c FROM Child c LEFT JOIN FETCH c.parent p
JPQL 2-Result: It return testChild1 whose parent is properly hydrated as testParent1
The function that inserts the data:
Code:
@Transactional
public Child updateChild(Child child) {
Parent parent = getExistingParent( child.getParent() );
child = entityManager.merge(child);
parent.getChildren().add( child );
entityManager.flush();
return child;
}
Any help is greatly appreciated.