use of jodd.db.oom.tst.Boy2 in project jodd by oblac.
the class DbHintTest method testHint.
@Test
public void testHint() {
DbSession dbSession = new DbThreadSession(cp);
// prepare data
assertEquals(1, DbEntitySql.insert(new Girl(1, "Anna", "swim")).query().executeUpdate());
assertEquals(1, DbEntitySql.insert(new Girl(2, "Sandra", "piano")).query().executeUpdate());
assertEquals(1, DbEntitySql.insert(new Boy2(1, "John", 1)).query().executeUpdate());
// select without hint
DbOomQuery dbOomQuery = new DbOomQuery(q1);
Object[] result = dbOomQuery.find(Boy2.class, Girl.class);
Boy2 boy2 = (Boy2) result[0];
assertEquals(1, boy2.id);
assertEquals("John", boy2.name);
assertEquals(1, boy2.girlId);
assertNull(boy2.girl);
Girl girl = (Girl) result[1];
assertEquals(1, girl.id);
// select with t-sql hint
dbOomQuery = new DbOomQuery(q2);
boy2 = dbOomQuery.find(Boy2.class, Girl.class);
assertEquals(1, boy2.id);
assertEquals("John", boy2.name);
assertEquals(1, boy2.girlId);
assertNotNull(boy2.girl);
assertEquals(1, boy2.girl.id);
assertEquals(0, boy2.totalGirls);
// select with external hints
dbOomQuery = new DbOomQuery(q3);
dbOomQuery.withHints("boy", "boy.girlAlt", "boy.totalGirls");
boy2 = dbOomQuery.find(Boy2.class, Girl.class, Integer.class);
assertEquals(1, boy2.id);
assertEquals("John", boy2.name);
assertEquals(1, boy2.girlId);
assertNotNull(boy2.girlAlt);
assertEquals(1, boy2.girlAlt.id);
assertEquals(2, boy2.totalGirls);
// same select with t-sql hints
dbOomQuery = new DbOomQuery(q4);
boy2 = dbOomQuery.find(Boy2.class, Girl.class);
assertEquals(1, boy2.id);
assertEquals("John", boy2.name);
assertEquals(1, boy2.girlId);
assertNotNull(boy2.girlAlt);
assertEquals(1, boy2.girlAlt.id);
assertEquals(0, boy2.totalGirls);
// same select with t-sql hints
dbOomQuery = new DbOomQuery(q5);
boy2 = dbOomQuery.find(Boy2.class, Girl.class, Integer.class);
assertEquals(1, boy2.id);
assertEquals("John", boy2.name);
assertEquals(1, boy2.girlId);
assertNotNull(boy2.girlAlt);
assertEquals(1, boy2.girlAlt.id);
assertEquals(2, boy2.totalGirls);
// same select with t-sql hints
dbOomQuery = new DbOomQuery(q6);
boy2 = dbOomQuery.find(Boy2.class, Girl.class);
assertEquals(1, boy2.id);
assertEquals("John", boy2.name);
assertEquals(1, boy2.girlId);
assertNotNull(boy2.girlAlt);
assertEquals(1, boy2.girlAlt.id);
assertNotNull(boy2.girlAlt.name);
assertNull(boy2.girlAlt.speciality);
dbSession.closeSession();
}
Aggregations