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.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
|
||||||
|
@ -31,31 +33,36 @@ public class EntityJoinTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
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 {
|
|
||||||
// testInnerEntityJoins();
|
|
||||||
testOuterEntityJoins();
|
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
|
@After
|
||||||
|
public void cleanup() {
|
||||||
deleteTestData();
|
deleteTestData();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void testInnerEntityJoins() {
|
@SuppressWarnings("unchecked")
|
||||||
|
@Test
|
||||||
|
public void testInnerEntityJoins() {
|
||||||
Session session = openSession();
|
Session session = openSession();
|
||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
|
|
||||||
try {
|
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 " +
|
"select r.id, c.name, u.id, u.username " +
|
||||||
"from FinancialRecord r " +
|
"from FinancialRecord r " +
|
||||||
" inner join r.customer c " +
|
" inner join r.customer c " +
|
||||||
" inner join User u on r.lastUpdateBy = u.username"
|
" inner join User u on r.lastUpdateBy = u.username"
|
||||||
).list();
|
).list();
|
||||||
|
|
||||||
assertThat(result.size(), is(1));
|
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 /
|
// NOTE that this leads to not really valid SQL, although some databases might support it /
|
||||||
// result = session.createQuery(
|
// result = session.createQuery(
|
||||||
|
@ -65,36 +72,67 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testOuterEntityJoins() {
|
@Test
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public void testLeftOuterEntityJoins() {
|
||||||
Session session = openSession();
|
Session session = openSession();
|
||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
List result = session.createQuery(
|
// this should get all financial records even if their lastUpdateBy user is null
|
||||||
"select r.id, c.name, u.id, u.username " +
|
List<Object[]> result = session.createQuery(
|
||||||
|
"select r.id, u.id, u.username " +
|
||||||
"from FinancialRecord r " +
|
"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();
|
).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 /
|
Object[] stevesRecord = result.get(0);
|
||||||
// result = session.createQuery(
|
assertThat(stevesRecord[0], is(1));
|
||||||
// "select r.id, r.customer.name, u.id, u.username " +
|
assertThat(stevesRecord[2], is("steve"));
|
||||||
// "from FinancialRecord r " +
|
|
||||||
// " left join User u on r.lastUpdateBy = u.username"
|
Object[] noOnesRecord = result.get(1);
|
||||||
// ).list();
|
assertThat(noOnesRecord[0], is(2));
|
||||||
// assertThat( result.size(), is( 1 ) );
|
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.getTransaction().commit();
|
||||||
session.close();
|
session.close();
|
||||||
}
|
}
|
||||||
|
@ -109,6 +147,7 @@ public class EntityJoinTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
final Customer customer = new Customer(1, "Acme");
|
final Customer customer = new Customer(1, "Acme");
|
||||||
session.save(customer);
|
session.save(customer);
|
||||||
session.save(new FinancialRecord(1, customer, "steve"));
|
session.save(new FinancialRecord(1, customer, "steve"));
|
||||||
|
session.save(new FinancialRecord(2, customer, null));
|
||||||
|
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
session.close();
|
session.close();
|
||||||
|
|
Loading…
Reference in New Issue