use of jodd.db.oom.tst.Girl4 in project jodd by oblac.
the class DbHint2Test method testHint.
@Test
public void testHint() {
DbSession dbSession = new DbThreadSession(cp);
// prepare data
assertEquals(1, DbEntitySql.insert(new Room(1, "Room1")).query().executeUpdate());
assertEquals(1, DbEntitySql.insert(new Room(2, "Room2")).query().executeUpdate());
assertEquals(1, DbEntitySql.insert(new Boy4(1, 1, "Oleg")).query().executeUpdate());
assertEquals(1, DbEntitySql.insert(new Boy4(2, 2, "Stephene")).query().executeUpdate());
assertEquals(1, DbEntitySql.insert(new Boy4(3, 2, "Joe")).query().executeUpdate());
assertEquals(1, DbEntitySql.insert(new Girl4(1, 1, "Anna")).query().executeUpdate());
assertEquals(1, DbEntitySql.insert(new Girl4(2, 2, "Sandra")).query().executeUpdate());
assertEquals(1, DbEntitySql.insert(new Girl4(3, 3, "Jossy")).query().executeUpdate());
// select
DbOomQuery dbOomQuery = new DbOomQuery(sql("select $C{room.*}, $C{boy.*}, $C{girl.*} " + "from $T{Room room} join $T{Boy4 boy} on $room.id=$boy.roomId " + "join $T{Girl4 girl} on $boy.id=$girl.boyId " + "order by $room.id, $boy.id"));
List<Object[]> results = dbOomQuery.autoClose().list(Room.class, Boy4.class, Girl4.class);
assertEquals(3, results.size());
Object[] row = results.get(0);
assertEquals(1, ((Room) row[0]).getId().longValue());
assertEquals(1, ((Boy4) row[1]).id.longValue());
assertEquals(1, ((Girl4) row[2]).getId().longValue());
// ---------------------------------------------------------------- hints
dbOomQuery = new DbOomQuery(sql("select $C{room.*}, $C{room.boys:boy.*}, $C{room.boys.girl:girl.*} " + "from $T{Room room} join $T{Boy4 boy} on $room.id=$boy.roomId " + "join $T{Girl4 girl} on $boy.id=$girl.boyId " + "order by $room.id, $boy.id"));
dbOomQuery.entityAwareMode(true).autoClose();
List<Room> rooms = dbOomQuery.list(Room.class, Boy4.class, Girl4.class);
assertEquals(2, rooms.size());
// room #1
Room room1 = rooms.get(0);
assertEquals(1, room1.getId().longValue());
assertEquals(1, room1.getBoys().size());
Boy4 boy1 = room1.getBoys().get(0);
assertEquals(1, boy1.id.intValue());
Girl4 girl1 = boy1.girl;
assertEquals(1, girl1.getId().longValue());
// room #2
Room room2 = rooms.get(1);
assertEquals(2, room2.getId().longValue());
assertEquals(2, room2.getBoys().size());
Boy4 boy2 = room2.getBoys().get(0);
Boy4 boy3 = room2.getBoys().get(1);
assertEquals(2, boy2.id.longValue());
assertEquals(3, boy3.id.longValue());
Girl4 girl2 = boy2.girl;
assertEquals(2, girl2.getId().longValue());
Girl4 girl3 = boy3.girl;
assertEquals(3, girl3.getId().longValue());
dbSession.closeSession();
}