| https://github.com/schauder/issue-datajpa-1404-is-null-on-one-to-one/tree/hibernate demonstrates the issue. Given two entities as follows
@Entity
public class Parent {
@Id
Long id;
@OneToOne(mappedBy = "parent")
Child child;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
and
@Entity
public class Child {
@Id
Long id;
@OneToOne
Parent parent;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
The following test fails, because the query returns an empty list:
Parent p = new Parent();
p.setId(23L);
entityManager.persist(p);
entityManager.flush();
List result = entityManager.createQuery("select p from Parent p where p.child is null").getResultList();
Assert.assertEquals(1, result.size());
Making the reference unidirectional going from Parent to Child results in the query resulting in a single result as expected. This is related to https://jira.spring.io/projects/DATAJPA/issues/DATAJPA-1404 The OP there indicated that DATAJPA-1404 did not appear in earlier versions of Spring Data, but that was possibly not because the query working in Hibernate, but Spring Data using a different query. Also, since Spring Data generates these queries via Criteria API I expect the same problem also to occur when using the Criteria API to construct an equivalent query. |