" Consider the two entities:
\r\n\r\n {code:java} class \r\nclass Parent { \r\n @Id \r\n private long id;
\r\n\r\n @OneToOne(mappedBy = \ "parent \ ") \r\n private Child child; \r\n } \r\n {code} and
\r\nand\r\n\r\n {code:java} \r\n @Entity class \r\nclass Child { \r\n @Id \r\n private long id;
\r\n\r\n @OneToOne \r\n private Parent parent; \r\n } \r\n {code}
The following \r\n\r\nThe JPQL-query \ "from Parent p where p.child is null \ " should list all parents that don't have a child. The \r\nThe actual SQL that is generated looks like this: \r\n {code:sql} select \r\nselect\r\n parent0_.id as id1_1_ \r\n from \r\n Parent parent0_ \r\n where \r\n parent0_.id is null \r\n {code} This \r\nThis query always returns an empty result, because the pk of person is never null!
Testcase \r\n\r\nTestcase :
\r\n\r\n {code:java} package \r\npackage org.hibernate.bugs;
import \r\n\r\nimport static org.assertj.core.api.Assertions.assertThat;
import \r\n\r\nimport java.util.List;
import \r\n\r\nimport javax.persistence.CascadeType; import \r\nimport javax.persistence.Entity; import \r\nimport javax.persistence.EntityManager; import \r\nimport javax.persistence.EntityManagerFactory; import \r\nimport javax.persistence.Id; import \r\nimport javax.persistence.OneToOne; import \r\nimport javax.persistence.Persistence; import \r\nimport javax.persistence.Query;
import \r\n\r\nimport org.junit.After; import \r\nimport org.junit.Before; import \r\nimport org.junit.Test;
\r\n\r\n /** \r\n * This template demonstrates how to develop a test case for Hibernate ORM, using the Java Persistence API. \r\n */ public \r\npublic class JPAUnitTestCase {
\r\n\r\n private EntityManagerFactory entityManagerFactory;
\r\n\r\n @Before \r\n public void init() { \r\n entityManagerFactory = Persistence.createEntityManagerFactory( \ "templatePU \ "); \r\n }
\r\n\r\n @After \r\n public void destroy() { \r\n entityManagerFactory.close(); \r\n }
\r\n\r\n // Entities are auto-discovered, so just add them anywhere on class-path \r\n // Add your tests, using standard JUnit. \r\n @Test \r\n public void hhh123Test() throws Exception { \r\n EntityManager entityManager = entityManagerFactory.createEntityManager();
\r\n\r\n runInTransaction(entityManager, () -> { \r\n Parent parent1 = new Parent(1); \r\n new Child(3, parent1); \r\n entityManager.persist(parent1);
\r\n\r\n Parent parent2 = new Parent(2); \r\n entityManager.persist(parent2); \r\n });
\r\n\r\n runInTransaction(entityManager, () -> { \r\n Query query = entityManager.createQuery( \ "from Parent p where p.child is null \ "); \r\n @SuppressWarnings( \ "unchecked \ ") \r\n List<Parent> parents = query.getResultList(); \r\n assertThat(parents).hasSize(1); \r\n assertThat(parents).element(0).extracting(Parent::getId).isEqualTo(2); \r\n });
\r\n\r\n entityManager.close(); \r\n }
\r\n\r\n private void runInTransaction(EntityManager entityManager, Runnable runnable) { \r\n entityManager.getTransaction().begin(); \r\n runnable.run(); \r\n entityManager.getTransaction().commit(); \r\n entityManager.clear(); \r\n } \r\n }
\r\n\r\n\r\n @Entity class \r\nclass Parent { \r\n @Id \r\n private long id;
\r\n\r\n @OneToOne(mappedBy = \ "parent \ ", cascade = CascadeType.ALL) \r\n private Child child;
\r\n\r\n Parent() {}
\r\n\r\n public Parent(long id) { \r\n this.id = id; \r\n }
\r\n\r\n public long getId() { \r\n return id; \r\n }
\r\n\r\n public Child getChild() { \r\n return child; \r\n }
\r\n\r\n void setChild(Child child) { \r\n this.child = child; \r\n }
\r\n\r\n @Override \r\n public String toString() { \r\n return \ "Parent [id= \ " + id + \ ", child= \ " + child + \ "] \ "; \r\n } \r\n }
\r\n\r\n\r\n @Entity class \r\nclass Child { \r\n @Id \r\n private long id;
\r\n\r\n @OneToOne \r\n private Parent parent;
\r\n\r\n Child() {}
\r\n\r\n public Child(long id, Parent parent) { \r\n this.id = id; \r\n setParent(parent); \r\n }
\r\n\r\n public Parent getParent() { \r\n return parent; \r\n }
\r\n\r\n public void setParent(Parent parent) { \r\n this.parent = parent; \r\n parent.setChild(this); \r\n }
\r\n\r\n @Override \r\n public String toString() { \r\n return \ "Child [id= \ " + id + \ "] \ "; \r\n } \r\n } \r\n {code} " |
|