From 951cc68a3e62f50417a3be849fb6b48c68eaeea7 Mon Sep 17 00:00:00 2001 From: Jan Martiska Date: Thu, 1 Dec 2016 11:19:04 +0100 Subject: [PATCH] HHH-11295 - Some improvements to EntityJoinTest (cherry picked from commit cff4ea1ce63003db005fb5f528e37ca2ca7f549a) --- .../hibernate/test/hql/EntityJoinTest.java | 369 ++++++++++-------- 1 file changed, 204 insertions(+), 165 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/EntityJoinTest.java b/hibernate-core/src/test/java/org/hibernate/test/hql/EntityJoinTest.java index ad70067cb6..b7bde19d5e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/hql/EntityJoinTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/EntityJoinTest.java @@ -15,49 +15,56 @@ import javax.persistence.Table; import org.hibernate.Session; import org.hibernate.annotations.NaturalId; - import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; /** - * @author Steve Ebersole + * @author Steve Ebersole, Jan Martiska */ public class EntityJoinTest extends BaseNonConfigCoreFunctionalTestCase { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { FinancialRecord.class, User.class, Customer.class }; - } + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] {FinancialRecord.class, User.class, Customer.class}; + } - @Test - public void testEntityJoins() { - createTestData(); + @Before + public void prepare() { + createTestData(); + } - try { -// testInnerEntityJoins(); - testOuterEntityJoins(); - } - finally { - deleteTestData(); - } - } + @After + public void cleanup() { + deleteTestData(); + } - private void testInnerEntityJoins() { - Session session = openSession(); - session.beginTransaction(); + @SuppressWarnings("unchecked") + @Test + public void testInnerEntityJoins() { + Session session = openSession(); + session.beginTransaction(); - try { - List result = session.createQuery( - "select r.id, c.name, u.id, u.username " + - "from FinancialRecord r " + - " inner join r.customer c " + - " inner join User u on r.lastUpdateBy = u.username" - ).list(); - assertThat( result.size(), is( 1 ) ); + try { + // this should get financial records which have a lastUpdateBy user set + List result = session.createQuery( + "select r.id, c.name, u.id, u.username " + + "from FinancialRecord r " + + " inner join r.customer c " + + " inner join User u on r.lastUpdateBy = u.username" + ).list(); - // NOTE that this leads to not really valid SQL, although some databases might support it / + assertThat(result.size(), is(1)); + Object[] steveAndAcme = result.get(0); + assertThat(steveAndAcme[0], is(1)); + assertThat(steveAndAcme[1], is("Acme")); + assertThat(steveAndAcme[3], is("steve")); + + // NOTE that this leads to not really valid SQL, although some databases might support it / // result = session.createQuery( // "select r.id, r.customer.name, u.id, u.username " + // "from FinancialRecord r " + @@ -65,175 +72,207 @@ public class EntityJoinTest extends BaseNonConfigCoreFunctionalTestCase { // ).list(); // assertThat( result.size(), is( 1 ) ); - } - finally { - session.getTransaction().commit(); - session.close(); - } + } finally { + session.getTransaction().commit(); + session.close(); + } + } - } + @Test + @SuppressWarnings("unchecked") + public void testLeftOuterEntityJoins() { + Session session = openSession(); + session.beginTransaction(); - private void testOuterEntityJoins() { - Session session = openSession(); - session.beginTransaction(); + try { + // this should get all financial records even if their lastUpdateBy user is null + List result = session.createQuery( + "select r.id, u.id, u.username " + + "from FinancialRecord r " + + " left join User u on r.lastUpdateBy = u.username" + + " order by r.id" + ).list(); + assertThat(result.size(), is(2)); - try { - List result = session.createQuery( - "select r.id, c.name, u.id, u.username " + - "from FinancialRecord r " + - " inner join r.customer c " + - " left join User u on r.lastUpdateBy = u.username" - ).list(); - assertThat( result.size(), is( 1 ) ); + Object[] stevesRecord = result.get(0); + assertThat(stevesRecord[0], is(1)); + assertThat(stevesRecord[2], is("steve")); - // NOTE that this leads to not really valid SQL, although some databases might support it / -// result = session.createQuery( -// "select r.id, r.customer.name, u.id, u.username " + -// "from FinancialRecord r " + -// " left join User u on r.lastUpdateBy = u.username" -// ).list(); -// assertThat( result.size(), is( 1 ) ); - } - finally { - session.getTransaction().commit(); - session.close(); - } - } + Object[] noOnesRecord = result.get(1); + assertThat(noOnesRecord[0], is(2)); + assertNull(noOnesRecord[2]); - private void createTestData() { - Session session = openSession(); - session.getTransaction().begin(); + } finally { + session.getTransaction().commit(); + session.close(); + } + } - session.save( new User( 1, "steve") ); - session.save( new User( 2, "jane") ); - final Customer customer = new Customer( 1, "Acme" ); - session.save( customer ); - session.save( new FinancialRecord( 1, customer, "steve" ) ); + @Test + @SuppressWarnings("unchecked") + public void testRightOuterEntityJoins() { + Session session = openSession(); + session.beginTransaction(); - session.getTransaction().commit(); - session.close(); - } + try { + // this should get all users even if they have no financial records + List result = session.createQuery( + "select r.id, u.id, u.username " + + "from FinancialRecord r " + + " right join User u on r.lastUpdateBy = u.username" + + " order by u.id" + ).list(); - private void deleteTestData() { - Session session = openSession(); - session.getTransaction().begin(); + assertThat(result.size(), is(2)); - session.createQuery( "delete FinancialRecord" ).executeUpdate(); - session.createQuery( "delete Customer" ).executeUpdate(); - session.createQuery( "delete User" ).executeUpdate(); + Object[] steveAndAcme = result.get(0); + assertThat(steveAndAcme[0], is(1)); + assertThat(steveAndAcme[2], is("steve")); - session.getTransaction().commit(); - session.close(); - } + Object[] janeAndNull = result.get(1); + assertNull(janeAndNull[0]); + assertThat(janeAndNull[2], is("jane")); + } finally { + session.getTransaction().commit(); + session.close(); + } + } - @Entity(name = "Customer") - @Table(name = "customer") - public static class Customer { - private Integer id; - private String name; + private void createTestData() { + Session session = openSession(); + session.getTransaction().begin(); - public Customer() { - } + session.save(new User(1, "steve")); + session.save(new User(2, "jane")); + final Customer customer = new Customer(1, "Acme"); + session.save(customer); + session.save(new FinancialRecord(1, customer, "steve")); + session.save(new FinancialRecord(2, customer, null)); - public Customer(Integer id, String name) { - this.id = id; - this.name = name; - } + session.getTransaction().commit(); + session.close(); + } - @Id - public Integer getId() { - return id; - } + private void deleteTestData() { + Session session = openSession(); + session.getTransaction().begin(); - public void setId(Integer id) { - this.id = id; - } + session.createQuery("delete FinancialRecord").executeUpdate(); + session.createQuery("delete Customer").executeUpdate(); + session.createQuery("delete User").executeUpdate(); - public String getName() { - return name; - } + session.getTransaction().commit(); + session.close(); + } - public void setName(String name) { - this.name = name; - } - } + @Entity(name = "Customer") + @Table(name = "customer") + public static class Customer { + private Integer id; + private String name; - @Entity(name = "FinancialRecord") - @Table(name = "financial_record") - public static class FinancialRecord { - private Integer id; - private Customer customer; - private String lastUpdateBy; + public Customer() { + } - public FinancialRecord() { - } + public Customer(Integer id, String name) { + this.id = id; + this.name = name; + } - public FinancialRecord(Integer id, Customer customer, String lastUpdateBy) { - this.id = id; - this.customer = customer; - this.lastUpdateBy = lastUpdateBy; - } + @Id + public Integer getId() { + return id; + } - @Id - public Integer getId() { - return id; - } + public void setId(Integer id) { + this.id = id; + } - public void setId(Integer id) { - this.id = id; - } + public String getName() { + return name; + } - @ManyToOne - @JoinColumn - public Customer getCustomer() { - return customer; - } + public void setName(String name) { + this.name = name; + } + } - public void setCustomer(Customer customer) { - this.customer = customer; - } + @Entity(name = "FinancialRecord") + @Table(name = "financial_record") + public static class FinancialRecord { + private Integer id; + private Customer customer; + private String lastUpdateBy; - public String getLastUpdateBy() { - return lastUpdateBy; - } + public FinancialRecord() { + } - public void setLastUpdateBy(String lastUpdateBy) { - this.lastUpdateBy = lastUpdateBy; - } - } + public FinancialRecord(Integer id, Customer customer, String lastUpdateBy) { + this.id = id; + this.customer = customer; + this.lastUpdateBy = lastUpdateBy; + } - @Entity(name = "User") - @Table(name = "`user`") - public static class User { - private Integer id; - private String username; + @Id + public Integer getId() { + return id; + } - public User() { - } + public void setId(Integer id) { + this.id = id; + } - public User(Integer id, String username) { - this.id = id; - this.username = username; - } + @ManyToOne + @JoinColumn + public Customer getCustomer() { + return customer; + } - @Id - public Integer getId() { - return id; - } + public void setCustomer(Customer customer) { + this.customer = customer; + } - public void setId(Integer id) { - this.id = id; - } + public String getLastUpdateBy() { + return lastUpdateBy; + } - @NaturalId - public String getUsername() { - return username; - } + public void setLastUpdateBy(String lastUpdateBy) { + this.lastUpdateBy = lastUpdateBy; + } + } - public void setUsername(String username) { - this.username = username; - } - } + @Entity(name = "User") + @Table(name = "`user`") + public static class User { + private Integer id; + private String username; + + public User() { + } + + public User(Integer id, String username) { + this.id = id; + this.username = username; + } + + @Id + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + @NaturalId + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + } }