HHH-11295 - Some improvements to EntityJoinTest

(cherry picked from commit cff4ea1ce6)
This commit is contained in:
Jan Martiska 2016-12-01 11:19:04 +01:00 committed by Gail Badner
parent 7b8a113fed
commit 951cc68a3e
1 changed files with 204 additions and 165 deletions

View File

@ -15,49 +15,56 @@ import javax.persistence.Table;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.annotations.NaturalId; import org.hibernate.annotations.NaturalId;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/** /**
* @author Steve Ebersole * @author Steve Ebersole, Jan Martiska
*/ */
public class EntityJoinTest extends BaseNonConfigCoreFunctionalTestCase { public class EntityJoinTest extends BaseNonConfigCoreFunctionalTestCase {
@Override @Override
protected Class[] getAnnotatedClasses() { protected Class[] getAnnotatedClasses() {
return new Class[] { FinancialRecord.class, User.class, Customer.class }; return new Class[] {FinancialRecord.class, User.class, Customer.class};
} }
@Test @Before
public void testEntityJoins() { public void prepare() {
createTestData(); createTestData();
}
try { @After
// testInnerEntityJoins(); public void cleanup() {
testOuterEntityJoins(); deleteTestData();
} }
finally {
deleteTestData();
}
}
private void testInnerEntityJoins() { @SuppressWarnings("unchecked")
Session session = openSession(); @Test
session.beginTransaction(); public void testInnerEntityJoins() {
Session session = openSession();
session.beginTransaction();
try { try {
List result = session.createQuery( // this should get financial records which have a lastUpdateBy user set
"select r.id, c.name, u.id, u.username " + List<Object[]> result = session.createQuery(
"from FinancialRecord r " + "select r.id, c.name, u.id, u.username " +
" inner join r.customer c " + "from FinancialRecord r " +
" inner join User u on r.lastUpdateBy = u.username" " inner join r.customer c " +
).list(); " inner join User u on r.lastUpdateBy = u.username"
assertThat( result.size(), is( 1 ) ); ).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( // result = session.createQuery(
// "select r.id, r.customer.name, u.id, u.username " + // "select r.id, r.customer.name, u.id, u.username " +
// "from FinancialRecord r " + // "from FinancialRecord r " +
@ -65,175 +72,207 @@ public class EntityJoinTest extends BaseNonConfigCoreFunctionalTestCase {
// ).list(); // ).list();
// assertThat( result.size(), is( 1 ) ); // assertThat( result.size(), is( 1 ) );
} } finally {
finally { session.getTransaction().commit();
session.getTransaction().commit(); session.close();
session.close(); }
} }
} @Test
@SuppressWarnings("unchecked")
public void testLeftOuterEntityJoins() {
Session session = openSession();
session.beginTransaction();
private void testOuterEntityJoins() { try {
Session session = openSession(); // this should get all financial records even if their lastUpdateBy user is null
session.beginTransaction(); List<Object[]> 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 { Object[] stevesRecord = result.get(0);
List result = session.createQuery( assertThat(stevesRecord[0], is(1));
"select r.id, c.name, u.id, u.username " + assertThat(stevesRecord[2], is("steve"));
"from FinancialRecord r " +
" inner join r.customer c " +
" left join User u on r.lastUpdateBy = u.username"
).list();
assertThat( result.size(), is( 1 ) );
// NOTE that this leads to not really valid SQL, although some databases might support it / Object[] noOnesRecord = result.get(1);
// result = session.createQuery( assertThat(noOnesRecord[0], is(2));
// "select r.id, r.customer.name, u.id, u.username " + assertNull(noOnesRecord[2]);
// "from FinancialRecord r " +
// " left join User u on r.lastUpdateBy = u.username"
// ).list();
// assertThat( result.size(), is( 1 ) );
}
finally {
session.getTransaction().commit();
session.close();
}
}
private void createTestData() { } finally {
Session session = openSession(); session.getTransaction().commit();
session.getTransaction().begin(); session.close();
}
}
session.save( new User( 1, "steve") ); @Test
session.save( new User( 2, "jane") ); @SuppressWarnings("unchecked")
final Customer customer = new Customer( 1, "Acme" ); public void testRightOuterEntityJoins() {
session.save( customer ); Session session = openSession();
session.save( new FinancialRecord( 1, customer, "steve" ) ); session.beginTransaction();
session.getTransaction().commit(); try {
session.close(); // this should get all users even if they have no financial records
} List<Object[]> 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() { assertThat(result.size(), is(2));
Session session = openSession();
session.getTransaction().begin();
session.createQuery( "delete FinancialRecord" ).executeUpdate(); Object[] steveAndAcme = result.get(0);
session.createQuery( "delete Customer" ).executeUpdate(); assertThat(steveAndAcme[0], is(1));
session.createQuery( "delete User" ).executeUpdate(); assertThat(steveAndAcme[2], is("steve"));
session.getTransaction().commit(); Object[] janeAndNull = result.get(1);
session.close(); assertNull(janeAndNull[0]);
} assertThat(janeAndNull[2], is("jane"));
} finally {
session.getTransaction().commit();
session.close();
}
}
@Entity(name = "Customer") private void createTestData() {
@Table(name = "customer") Session session = openSession();
public static class Customer { session.getTransaction().begin();
private Integer id;
private String name;
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) { session.getTransaction().commit();
this.id = id; session.close();
this.name = name; }
}
@Id private void deleteTestData() {
public Integer getId() { Session session = openSession();
return id; session.getTransaction().begin();
}
public void setId(Integer id) { session.createQuery("delete FinancialRecord").executeUpdate();
this.id = id; session.createQuery("delete Customer").executeUpdate();
} session.createQuery("delete User").executeUpdate();
public String getName() { session.getTransaction().commit();
return name; session.close();
} }
public void setName(String name) { @Entity(name = "Customer")
this.name = name; @Table(name = "customer")
} public static class Customer {
} private Integer id;
private String name;
@Entity(name = "FinancialRecord") public Customer() {
@Table(name = "financial_record") }
public static class FinancialRecord {
private Integer id;
private Customer customer;
private String lastUpdateBy;
public FinancialRecord() { public Customer(Integer id, String name) {
} this.id = id;
this.name = name;
}
public FinancialRecord(Integer id, Customer customer, String lastUpdateBy) { @Id
this.id = id; public Integer getId() {
this.customer = customer; return id;
this.lastUpdateBy = lastUpdateBy; }
}
@Id public void setId(Integer id) {
public Integer getId() { this.id = id;
return id; }
}
public void setId(Integer id) { public String getName() {
this.id = id; return name;
} }
@ManyToOne public void setName(String name) {
@JoinColumn this.name = name;
public Customer getCustomer() { }
return customer; }
}
public void setCustomer(Customer customer) { @Entity(name = "FinancialRecord")
this.customer = customer; @Table(name = "financial_record")
} public static class FinancialRecord {
private Integer id;
private Customer customer;
private String lastUpdateBy;
public String getLastUpdateBy() { public FinancialRecord() {
return lastUpdateBy; }
}
public void setLastUpdateBy(String lastUpdateBy) { public FinancialRecord(Integer id, Customer customer, String lastUpdateBy) {
this.lastUpdateBy = lastUpdateBy; this.id = id;
} this.customer = customer;
} this.lastUpdateBy = lastUpdateBy;
}
@Entity(name = "User") @Id
@Table(name = "`user`") public Integer getId() {
public static class User { return id;
private Integer id; }
private String username;
public User() { public void setId(Integer id) {
} this.id = id;
}
public User(Integer id, String username) { @ManyToOne
this.id = id; @JoinColumn
this.username = username; public Customer getCustomer() {
} return customer;
}
@Id public void setCustomer(Customer customer) {
public Integer getId() { this.customer = customer;
return id; }
}
public void setId(Integer id) { public String getLastUpdateBy() {
this.id = id; return lastUpdateBy;
} }
@NaturalId public void setLastUpdateBy(String lastUpdateBy) {
public String getUsername() { this.lastUpdateBy = lastUpdateBy;
return username; }
} }
public void setUsername(String username) { @Entity(name = "User")
this.username = username; @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;
}
}
} }