HHH-11295 - Some improvements to EntityJoinTest
(cherry picked from commit cff4ea1ce6
)
This commit is contained in:
parent
7b8a113fed
commit
951cc68a3e
|
@ -15,15 +15,17 @@ 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
|
||||
|
@ -31,31 +33,36 @@ public class EntityJoinTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
return new Class[] {FinancialRecord.class, User.class, Customer.class};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEntityJoins() {
|
||||
@Before
|
||||
public void prepare() {
|
||||
createTestData();
|
||||
|
||||
try {
|
||||
// testInnerEntityJoins();
|
||||
testOuterEntityJoins();
|
||||
}
|
||||
finally {
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
deleteTestData();
|
||||
}
|
||||
}
|
||||
|
||||
private void testInnerEntityJoins() {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testInnerEntityJoins() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
|
||||
try {
|
||||
List result = session.createQuery(
|
||||
// this should get financial records which have a lastUpdateBy user set
|
||||
List<Object[]> 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));
|
||||
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(
|
||||
|
@ -65,36 +72,67 @@ public class EntityJoinTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
// ).list();
|
||||
// assertThat( result.size(), is( 1 ) );
|
||||
|
||||
}
|
||||
finally {
|
||||
} finally {
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void testOuterEntityJoins() {
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testLeftOuterEntityJoins() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
|
||||
try {
|
||||
List result = session.createQuery(
|
||||
"select r.id, c.name, u.id, u.username " +
|
||||
// this should get all financial records even if their lastUpdateBy user is null
|
||||
List<Object[]> result = session.createQuery(
|
||||
"select r.id, u.id, u.username " +
|
||||
"from FinancialRecord r " +
|
||||
" inner join r.customer c " +
|
||||
" left join User u on r.lastUpdateBy = u.username"
|
||||
" left join User u on r.lastUpdateBy = u.username" +
|
||||
" order by r.id"
|
||||
).list();
|
||||
assertThat( result.size(), is( 1 ) );
|
||||
assertThat(result.size(), is(2));
|
||||
|
||||
// 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 ) );
|
||||
Object[] stevesRecord = result.get(0);
|
||||
assertThat(stevesRecord[0], is(1));
|
||||
assertThat(stevesRecord[2], is("steve"));
|
||||
|
||||
Object[] noOnesRecord = result.get(1);
|
||||
assertThat(noOnesRecord[0], is(2));
|
||||
assertNull(noOnesRecord[2]);
|
||||
|
||||
} finally {
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
finally {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testRightOuterEntityJoins() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
|
||||
try {
|
||||
// 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();
|
||||
|
||||
assertThat(result.size(), is(2));
|
||||
|
||||
Object[] steveAndAcme = result.get(0);
|
||||
assertThat(steveAndAcme[0], is(1));
|
||||
assertThat(steveAndAcme[2], is("steve"));
|
||||
|
||||
Object[] janeAndNull = result.get(1);
|
||||
assertNull(janeAndNull[0]);
|
||||
assertThat(janeAndNull[2], is("jane"));
|
||||
} finally {
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
@ -109,6 +147,7 @@ public class EntityJoinTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
final Customer customer = new Customer(1, "Acme");
|
||||
session.save(customer);
|
||||
session.save(new FinancialRecord(1, customer, "steve"));
|
||||
session.save(new FinancialRecord(2, customer, null));
|
||||
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
|
Loading…
Reference in New Issue