I'm a beginner to the JPA/JPQL stuff and I'm having problems fetching many-to-one relationships when I make the relationship bi-directional. Here is the JPQL:
select c from Child c join fetch c.parent
Here are the two simple classes:
@Entity
public class Parent {
@Id
private int id;
private String title;
@OneToMany(mappedBy = "parent")
private Set<Child> children;
}
@Entity
public class Child {
@Id
private int id;
@ManyToOne(fetch = FetchType.LAZY)
private Parent parent;
}
The equivalent SQL query executed by datanucleus is:
SELECT 'com.*.Child' AS NUCLEUS_TYPE,`C`.`ID`,`C`.`PARENT_ID` FROM `CHILD` `C` INNER JOIN `PARENT` `B0` ON `C`.`PARENT_ID` = `B0`.`ID`
Now if I completely remove the reference to "children" in Parent, the SQL is exactly what I need:
SELECT 'com.*.Child' AS NUCLEUS_TYPE,`C`.`ID`,`B0`.`ID`,`B0`.`TITLE` FROM `CHILD` `C` INNER JOIN `PARENT` `B0` ON `C`.`PARENT_ID` = `B0`.`ID`
To be clear: what I'm trying to achieve is to fetch the child's parent with my JPQL query.
Update: I just tried these two classes with EclipseLink and this works, so looks like this problem is Datanucleus-specific.