Package jodd.db

Examples of jodd.db.DbSession


    public abstract void initDb();
    public abstract String getCreateTableSql();
    public abstract String getTableName();

    public final void createTables() {
      DbSession session = new DbSession();

      String sql = getCreateTableSql();

      DbQuery query = new DbQuery(session, sql);
      query.executeUpdate();

      session.closeSession();
      assertTrue(query.isClosed());
    }
View Full Code Here


      session.closeSession();
      assertTrue(query.isClosed());
    }

    protected void close() {
      DbSession session = new DbSession();

      DbQuery query = new DbQuery(session, "drop table " + getTableName());
      query.executeUpdate();

      session.closeSession();
      assertTrue(query.isClosed());

      connectionPool.close();
    }
View Full Code Here

  }

  // ---------------------------------------------------------------- workout

  protected void workoutEntity() {
    DbSession session = new DbSession();

    Tester tester = new Tester();
    tester.setName("one");
    tester.setValue(Integer.valueOf(7));

    DbOomQuery dbOomQuery = DbOomQuery.query(session, DbEntitySql.insert(tester));
    dbOomQuery.setGeneratedKey();
    dbOomQuery.executeUpdate();
    assertDb(session, "{1,one,7}");

    long key = dbOomQuery.getGeneratedKey();
    tester.setId(Long.valueOf(key));
    dbOomQuery.close();

    assertEquals(1, tester.getId().longValue());

    tester.setName("seven");
    DbOomQuery.query(session, DbEntitySql.updateAll(tester)).executeUpdate();
    assertDb(session, "{1,seven,7}");

    tester.setName("SEVEN");
    DbOomQuery.query(session, DbEntitySql.update(tester)).executeUpdate();
    assertDb(session, "{1,SEVEN,7}");

    tester.setName("seven");
    DbOomQuery.query(session, DbEntitySql.updateColumn(tester, "name")).executeUpdate();
    assertDb(session, "{1,seven,7}");

    tester = new Tester();
    tester.setId(Long.valueOf(2));
    tester.setName("two");
    tester.setValue(Integer.valueOf(2));
    DbOomQuery.query(session, DbEntitySql.insert(tester)).executeUpdate();
    assertDb(session, "{1,seven,7}{2,two,2}");

    long count = DbOomQuery.query(session, DbEntitySql.count(Tester.class)).executeCount();
    assertEquals(2, count);

    tester = DbOomQuery.query(session, DbEntitySql.findById(Tester.class, 2)).find(Tester.class);
    assertNotNull(tester);
    assertEquals("{2,two,2}", tester.toString());

    tester = DbOomQuery
        .query(session, DbEntitySql
            .findById(Tester.class, 2)
            .aliasColumnsAs(ColumnAliasType.COLUMN_CODE))
        .find(Tester.class);
    assertNotNull(tester);
    assertEquals("{2,two,2}", tester.toString());

    tester = DbOomQuery
        .query(session, DbEntitySql
            .findById(Tester.class, 2)
            .aliasColumnsAs(ColumnAliasType.TABLE_REFERENCE))
        .find(Tester.class);
    assertNotNull(tester);
    assertEquals("{2,two,2}", tester.toString());

    tester = DbOomQuery
        .query(session, DbEntitySql
            .findById(Tester.class, 2)
            .aliasColumnsAs(ColumnAliasType.TABLE_NAME))
        .find(Tester.class);
    assertNotNull(tester);
    assertEquals("{2,two,2}", tester.toString());

    tester = DbOomQuery
        .query(session, DbEntitySql
            .findById(Tester.class, 2)
            .aliasColumnsAs(ColumnAliasType.COLUMN_CODE))    // fixes POSTGRESQL
        .find();
    assertEquals("{2,two,2}", tester.toString());

    tester = new Tester();
    tester.setName("seven");
    tester = DbOomQuery.query(session, DbEntitySql.find(tester)).find(Tester.class);
    assertEquals("{1,seven,7}", tester.toString());

    DbOomQuery.query(session, DbEntitySql.findByColumn(Tester.class, "name", "seven")).find(Tester.class);
    assertEquals("{1,seven,7}", tester.toString());

    DbOomQuery.query(session, DbEntitySql.deleteById(Tester.class, 1)).executeUpdate();

    count = DbOomQuery.query(session, DbEntitySql.count(Tester.class)).executeCount();
    assertEquals(1, count);

    session.closeSession();
  }
View Full Code Here

    dbOom.registerEntity(BadBoy.class);
  }

  @Test
  public void testOrm() {
    DbSession session = new DbThreadSession(cp);

    // ---------------------------------------------------------------- insert

    assertEquals(1, DbEntitySql.insert(new Girl(1, "Anna", "swim")).query().autoClose().executeUpdate());
    assertEquals(1, DbEntitySql.insert(new Girl(2, "Sandra", "piano")).query().autoClose().executeUpdate());
    assertEquals(0, session.getTotalQueries());

    DbOomQuery q2 = new DbOomQuery(DbEntitySql.insert(new Girl(3, "Monica", null)));
    q2.setDebugMode();
    assertEquals("insert into GIRL (ID, NAME) values (:girl.id, :girl.name)", q2.getQueryString());
    q2.init();
    assertEquals("insert into GIRL (ID, NAME) values (3, 'Monica')", q2.getQueryString());
    assertEquals(1, q2.autoClose().executeUpdate());
    assertTrue(q2.isClosed());

    assertEquals(1, DbEntitySql.insert(new BadBoy(Integer.valueOf(1), "Johny", Integer.valueOf(3))).query().autoClose().executeUpdate());
    assertEquals(0, session.getTotalQueries());

    DbQuery dq = new DbQuery("select count(*) from GIRL where id>:id");
    dq.setInteger("id", 1);
    assertEquals(2, dq.executeCount());

    //dq.reset();
    dq.setInteger("id", 10);
    assertEquals(1, session.getTotalQueries());
    assertEquals(0, dq.autoClose().executeCount());

    assertEquals(0, session.getTotalQueries());


    // ---------------------------------------------------------------- girl

    DbOomQuery q = new DbOomQuery("select * from GIRL order by ID");

    Girl girl = q.find(Girl.class);
    checkGirl1(girl);
    assertTrue(q.isActive());

    girl = q.find(new QueryMapper<Girl>() {
      public Girl process(ResultSet resultSet) throws SQLException {
        Girl _girl = new Girl();
        _girl.id = resultSet.getInt("ID");
        _girl.name = resultSet.getString("NAME");
        _girl.speciality = resultSet.getString("SPECIALITY");
        return _girl;
      }
    });

    checkGirl1(girl);
    assertTrue(q.isActive());

    IdName idName = q.find(IdName.class);
    assertNotNull(idName);
    assertEquals(1, idName.id);
    assertEquals("Anna", idName.name);


    girl = q.find(Girl.class);
    checkGirl1(girl);

    // list
    List<Girl> listGirl = q.list(Girl.class);
    assertEquals(3, listGirl.size());
    girl = listGirl.get(1);
    checkGirl2(girl);

    listGirl = q.list(
      new QueryMapper<Girl>() {
        public Girl process(ResultSet resultSet) throws SQLException {
          Girl _girl = new Girl();
          _girl.id = resultSet.getInt("ID");
          _girl.name = resultSet.getString("NAME");
          _girl.speciality = resultSet.getString("SPECIALITY");
          return _girl;
        }
      }
    );

    assertEquals(3, listGirl.size());
    girl = listGirl.get(1);
    checkGirl2(girl);

    listGirl = q.list();
    assertEquals(3, listGirl.size());
    girl = listGirl.get(1);
    checkGirl2(girl);

    List list = q.list(Girl.class);
    assertEquals(3, list.size());
    girl = (Girl) list.get(2);
    checkGirl3(girl);
    checkGirl2((Girl) list.get(1));
    checkGirl1((Girl) list.get(0));
    assertFalse(q.isClosed());

    list = q.list();
    assertEquals(3, list.size());
    girl = (Girl) list.get(2);
    checkGirl3(girl);
    checkGirl2((Girl) list.get(1));
    checkGirl1((Girl) list.get(0));
    assertFalse(q.isClosed());

    // set
    Set<Girl> setGirl = q.listSet(Girl.class);
    assertEquals(3, setGirl.size());
    girl = (setGirl.iterator().next());
    checkGirl1(girl);

    setGirl = q.listSet();
    assertEquals(3, setGirl.size());
    girl = (setGirl.iterator().next());
    checkGirl1(girl);

    Set set = q.listSet(Girl.class);
    assertEquals(3, set.size());
    Iterator iterator = set.iterator();
    assertTrue(iterator.hasNext());
    girl = (Girl) iterator.next();
    checkGirl1(girl);
    assertTrue(iterator.hasNext());
    girl = (Girl) iterator.next();
    checkGirl2(girl);
    assertTrue(iterator.hasNext());
    girl = (Girl) iterator.next();
    checkGirl3(girl);
    assertFalse(iterator.hasNext());

    set = q.listSet();
    assertEquals(3, set.size());
    iterator = set.iterator();
    assertTrue(iterator.hasNext());
    girl = (Girl) iterator.next();
    checkGirl1(girl);
    assertTrue(iterator.hasNext());
    girl = (Girl) iterator.next();
    checkGirl2(girl);
    assertTrue(iterator.hasNext());
    girl = (Girl) iterator.next();
    checkGirl3(girl);
    assertFalse(iterator.hasNext());

    // iterator
    Iterator<Girl> it = q.iterate(Girl.class);
    int count = 0;
    while (it.hasNext()) {
      girl = it.next();
      count++;
      switch (girl.id) {
        case 1: checkGirl1(girl); break;
        case 2: checkGirl2(girl); break;
        case 3: checkGirl3(girl); break;
      }
    }
    assertEquals(3, count);

    it = q.iterate();
    count = 0;
    while (it.hasNext()) {
      girl = it.next();
      count++;
      switch (girl.id) {
        case 1: checkGirl1(girl); break;
        case 2: checkGirl2(girl); break;
        case 3: checkGirl3(girl); break;
      }
    }
    assertEquals(3, count);

    Iterator it2 = q.iterate(Girl.class);
    while (it2.hasNext()) {
      girl = (Girl) it2.next();
    }
    checkGirl3(girl);

    it2 = q.iterate();
    while (it2.hasNext()) {
      girl = (Girl) it2.next();
    }
    checkGirl3(girl);

    q.close();

    // ---------------------------------------------------------------- girl2

    q = new DbOomQuery("select girl.*, girl.* from GIRL girl order by girl.ID");
    list = q.list(Girl.class, Girl.class);
    assertEquals(3, list.size());
    assertEquals(2, ((Object[]) list.get(2)).length);
    girl = (Girl) ((Object[]) list.get(2))[0];
    checkGirl3(girl);
    girl = (Girl) ((Object[]) list.get(2))[1];
    checkGirl3(girl);


    list = q.list();
    assertEquals(3, list.size());
    girl = (Girl) ((Object[]) list.get(2))[0];
    checkGirl3(girl);
    assertEquals(2, ((Object[]) list.get(2)).length);
    girl = (Girl) ((Object[]) list.get(2))[0];
    checkGirl3(girl);
    girl = (Girl) ((Object[]) list.get(2))[1];
    checkGirl3(girl);

    q.close();

    // ---------------------------------------------------------------- boy

    q = new DbOomQuery("select * from BOY order by ID");

    BadBoy badBoy = q.find(BadBoy.class);
    checkBoy(badBoy);

    badBoy = q.find();
    checkBoy(badBoy);

    badBoy = q.find(BadBoy.class);
    checkBoy(badBoy);

    badBoy = q.find();
    checkBoy(badBoy);


    // list
    List<BadBoy> listBadBoyt = q.list(BadBoy.class);
    assertEquals(1, listBadBoyt.size());
    badBoy = listBadBoyt.get(0);
    checkBoy(badBoy);

    list = q.list(BadBoy.class);
    assertEquals(1, list.size());
    badBoy = (BadBoy) list.get(0);
    checkBoy(badBoy);


    // set
    Set<BadBoy> setBadBoy = q.listSet(BadBoy.class);
    assertEquals(1, setBadBoy.size());
    badBoy = (setBadBoy.iterator().next());
    checkBoy(badBoy);

    set = q.listSet(BadBoy.class);
    assertEquals(1, set.size());
    badBoy = (BadBoy) set.iterator().next();
    checkBoy(badBoy);

    // iterator
    Iterator<BadBoy> itBad = q.iterate(BadBoy.class);
    while (itBad.hasNext()) {
      badBoy = itBad.next();
    }
    checkBoy(badBoy);

    Iterator itBad2 = q.iterate(BadBoy.class);
    while (itBad2.hasNext()) {
      badBoy = (BadBoy) itBad2.next();
    }
    checkBoy(badBoy);

    q.close();


    // ---------------------------------------------------------------- join


    q = new DbOomQuery("select * from GIRL join BOY on GIRL.ID=BOY.GIRL_ID");

    girl = q.find(Girl.class);
    checkGirl3(girl);

    Object[] res = q.find(Girl.class, BadBoy.class);
    badBoy = (BadBoy) res[1];
    girl = (Girl) res[0];
    checkGirl3(girl);
    checkBoy(badBoy);

    res = q.find();
    girl = (Girl) res[0];
    badBoy = (BadBoy) res[1];
    checkGirl3(girl);
    checkBoy(badBoy);

    q.close();


    q = new DbOomQuery(DbSqlBuilder.sql("select $C{girl.*}, $C{BadBoy.*} from $T{Girl girl} join $T{BadBoy} on girl.id=$BadBoy.girlId"));
    badBoy = q.withHints("BadBoy.girl, BadBoy").find();
    girl = badBoy.girl;
    checkGirl3(girl);
    checkBoy(badBoy);
    q.close();

    q = new DbOomQuery(DbSqlBuilder.sql("select $C{girl.*}, $C{BadBoy.*} from $T{Girl girl} join $T{BadBoy} on girl.id=$BadBoy.girlId"));
    List<BadBoy> boys1 = q.withHints("BadBoy.girl, BadBoy").list(Girl.class, BadBoy.class);
    assertEquals(1, boys1.size());
    badBoy = boys1.get(0);
    assertNotNull(badBoy);
    girl = badBoy.girl;
    checkGirl3(girl);
    checkBoy(badBoy);
    q.close();

    q = query(sql("select $C{BadBoy.girl.*}, $C{BadBoy.*} from $T{Girl girl} join $T{BadBoy} on girl.id=$BadBoy.girlId"));
    badBoy = q.find();
    girl = badBoy.girl;
    checkGirl3(girl);
    checkBoy(badBoy);
    q.close();


    // ---------------------------------------------------------------- join

    //q = new DbOomQuery("select * from GIRL, BOY where BOY.GIRL_ID=GIRL.ID");
    q = new DbOomQuery("select * from GIRL join BOY on GIRL.ID=BOY.GIRL_ID");

    badBoy = q.find(BadBoy.class);
    assertNull(badBoy);         // wrong mapping order, girl is first!

    BadGirl badGirl = q.find(BadGirl.class);
    checkBadGirl3(badGirl);

    res = q.find(BadBoy.class, BadGirl.class);
    badBoy = (BadBoy) res[0];
    badGirl = (BadGirl) res[1];
    checkBadGirl3(badGirl);
    assertNull(badBoy);     // order is invalid

    res = q.find(BadGirl.class, BadBoy.class);
    badBoy = (BadBoy) res[1];
    badGirl = (BadGirl) res[0];
    checkBadGirl3(badGirl);
    checkBoy(badBoy);

    res = q.find(Boy.class, Girl.class);
    Boy boy = (Boy) res[0];
    girl = (Girl) res[1];
    assertNull(boy);        // order is invalid
    checkGirl3(girl);

    res = q.find(Girl.class, Boy.class);
    boy = (Boy) res[1];
    girl = (Girl) res[0];
    checkBoy(boy);
    checkGirl3(girl);


    // ---------------------------------------------------------------- left join

    q = new DbOomQuery("select * from GIRL left join BOY on GIRL.ID=BOY.GIRL_ID order by GIRL.ID");

    list = q.list(Girl.class, Boy3.class);
    assertEquals(3, list.size());

    checkGirl1((Girl) ((Object[]) list.get(0))[0]);
    assertNull(((Object[]) list.get(0))[1]);

    checkGirl2((Girl) ((Object[]) list.get(1))[0]);
    assertNull(((Object[]) list.get(1))[1]);

    checkGirl3((Girl) ((Object[]) list.get(2))[0]);
    assertNotNull(((Object[]) list.get(2))[1]);            // BOY is set here


    //
    list = q.list(Girl.class, Boy.class);
    assertEquals(3, list.size());
    checkGirl1((Girl) ((Object[]) list.get(0))[0]);
    assertNull(((Object[]) list.get(0))[1]);                // empty boy is set here, since ID type is primitive int and it can't be null!!!

    checkGirl2((Girl) ((Object[]) list.get(1))[0]);
    assertNull(((Object[]) list.get(1))[1]);

    checkGirl3((Girl) ((Object[]) list.get(2))[0]);
    checkBoy((Boy) ((Object[]) list.get(2))[1]);

    // ---------------------------------------------------------------- match

    girl = new Girl();
    girl.id=1;
    girl.speciality = "run";    // new values

    Girl girl_condition = new Girl();
    girl_condition.speciality = "swim";

    String tableRef = "ggg";

    DbSqlBuilder dsb = sql()
        ._("select * from ")
        .table(girl, tableRef)
        ._(" where ")
        .match(tableRef, "conditionRef")
        .use("conditionRef", girl_condition);

    q = new DbOomQuery(dsb);

    list = q.list(Girl.class);

    assertEquals(1, list.size());
    checkGirl1((Girl) list.get(0));    // swim

    dsb = sql()
        ._("update ")
        .table(girl, tableRef)
        .set(tableRef, girl)
        ._(" where ")
        .match(tableRef, "conditionRef")
        .use("conditionRef", girl_condition);

    q = new DbOomQuery(dsb);

    assertEquals(1, q.executeUpdate());

    girl_condition.speciality = "run";

    dsb = sql()
        ._("select * from ")
        .table(girl, tableRef)
        ._(" where ")
        .match(tableRef, "conditionRef")
        .use("conditionRef", girl_condition);

    q = new DbOomQuery(dsb);
    list = q.list(Girl.class);

    assertEquals(1, list.size());
    assertEquals(1, ((Girl) list.get(0)).id);
    assertEquals("run", ((Girl) list.get(0)).speciality);    // run

    // go back to swim

    girl.speciality = "swim";
    girl_condition.speciality = "run";

    dsb = sql()
        ._("update ")
        .table(girl, tableRef)
        .set(tableRef, girl)
        ._(" where ")
        .match(tableRef, "conditionRef")
        .use("conditionRef", girl_condition);

    q = new DbOomQuery(dsb);

    assertEquals(1, q.executeUpdate());


    // ---------------------------------------------------------------- etc

    badGirl = new BadGirl();
    badBoy = new BadBoy();
    DbSqlBuilder dt = sql("select $C{g.*, b.*} from $T{g}, $T{b} where $M{g=g} and $M{b=b}").use("g", badGirl).use("b", badBoy);
    q = new DbOomQuery(dt);
    list = q.list(BadBoy.class, BadGirl.class);
    assertEquals(3, list.size());

    dt = sql("select $C{g.*, b.*} from $T{g}, $T{b} where $M{g=g} and $M{b=b}").use("g", badGirl).use("b", badBoy);
    q = new DbOomQuery(dt);
    list = q.list(BadBoy.class, BadGirl.class);
    assertEquals(3, list.size());

    dt = sql("select g.*, b.* from $T{g g}, $T{b b} where $M{b=b} and $M{g=g}").use("g", badGirl).use("b", badBoy);
    q = new DbOomQuery(dt);
    list = q.list(BadBoy.class, BadGirl.class);
    assertEquals(3, list.size());


    badGirl.fooname = "Sandra";
    dt = sql("select $C{g.*, b.*} from $T{g}, $T{b} where $M{b=b} and $M{g=g}").use("g", badGirl).use("b", badBoy);
    q = new DbOomQuery(dt);
    Object[] result = q.find(BadGirl.class, BadBoy.class);
    checkBoy((BadBoy) result[1]);
    checkBadGirl2((BadGirl) result[0]);

    dt = sql("select $C{g.*, b.*} from $T{g}, $T{b} where $M{b=b} and $M{g=g}").use("g", badGirl).use("b", badBoy);
    q = new DbOomQuery(dt);
    result = q.find(BadGirl.class, BadBoy.class);
    checkBoy((BadBoy) result[1]);
    checkBadGirl2((BadGirl) result[0]);

    dt = sql("select $C{b.*, g.*} from $T{g}, $T{b} where $M{g=g} and $M{b=b}").use("g", badGirl).use("b", badBoy);
    q = new DbOomQuery(dt);
    result = q.find(BadBoy.class, BadGirl.class);
    checkBoy((BadBoy) result[0]);
    checkBadGirl2((BadGirl) result[1]);


    badBoy.ajdi = Integer.valueOf(1);
    badBoy.nejm = "Johny";
    dt = sql("select b.*, g.* from $T{g g}, $T{b b} where $M{g=g} and $M{b=b}").use("g", badGirl).use("b", badBoy);
    q = new DbOomQuery(dt);
    result = q.find(BadBoy.class, BadGirl.class);
    checkBoy((BadBoy) result[0]);
    checkBadGirl2((BadGirl) result[1]);

    dt = sql("select $C{b.*, g.*} from $T{g}, $T{b} where $M{g=g} and $M{b=b}").use("g", badGirl).use("b", badBoy);
    q = new DbOomQuery(dt);
    result = q.find(BadBoy.class, BadGirl.class);
    checkBoy((BadBoy) result[0]);
    checkBadGirl2((BadGirl) result[1]);

    dt = sql("select b.*, g.* from $T{g g}, $T{b b} where $M{b=b} and $M{g=g}").use("g", badGirl).use("b", badBoy);
    q = new DbOomQuery(dt);
    result = q.find(BadBoy.class, BadGirl.class);
    checkBoy((BadBoy) result[0]);
    checkBadGirl2((BadGirl) result[1]);


    dt = sql("select $C{g.fooid}, $C{b.*} from $T{g}, $T{b} where $M{g=g} and $M{b=b}").use("g", badGirl).use("b", badBoy);
    q = new DbOomQuery(dt);
    result = q.find(BadGirl.class, BadBoy.class);
    badGirl = (BadGirl) result[0];
    checkBoy((BadBoy) result[1]);
    assertEquals(2, badGirl.fooid.intValue());
    assertNull(badGirl.fooname);
    assertNull(badGirl.foospeciality);


    // ---------------------------------------------------------------- special

    dt = sql("select $g.fooid * 2 as did, $C{g.+} from $T{g} order by $g.fooid").aliasColumnsAs(COLUMN_CODE).use("g", BadGirl.class);
    q = new DbOomQuery(dt);
    list = q.list(null, BadGirl.class); // explicitly ignore the first column 'did'
    assertEquals(3, list.size());
    assertEquals(1, ((BadGirl) ((Object[]) list.get(0))[1]).fooid.intValue());
    assertEquals(2, ((BadGirl) ((Object[]) list.get(1))[1]).fooid.intValue());
    assertEquals(3, ((BadGirl) ((Object[]) list.get(2))[1]).fooid.intValue());

    list = q.list(Integer.class, BadGirl.class);
    assertEquals(3, list.size());
    assertEquals(2, ((Integer) ((Object[]) list.get(0))[0]).intValue());
    assertEquals(1, ((BadGirl) ((Object[]) list.get(0))[1]).fooid.intValue());
    assertEquals(4, ((Integer) ((Object[]) list.get(1))[0]).intValue());
    assertEquals(2, ((BadGirl) ((Object[]) list.get(1))[1]).fooid.intValue());
    assertEquals(6, ((Integer) ((Object[]) list.get(2))[0]).intValue());
    assertEquals(3, ((BadGirl) ((Object[]) list.get(2))[1]).fooid.intValue());


    q = new DbOomQuery("select g.ID * 2 as did, g.ID from Girl g order by g.ID");
    list = q.list(Integer.class, BadGirl.class);
    assertEquals(3, list.size());
    assertEquals(2, ((Integer) ((Object[]) list.get(0))[0]).intValue());
    assertEquals(1, ((BadGirl) ((Object[]) list.get(0))[1]).fooid.intValue());
    assertEquals(4, ((Integer) ((Object[]) list.get(1))[0]).intValue());
    assertEquals(2, ((BadGirl) ((Object[]) list.get(1))[1]).fooid.intValue());
    assertEquals(6, ((Integer) ((Object[]) list.get(2))[0]).intValue());
    assertEquals(3, ((BadGirl) ((Object[]) list.get(2))[1]).fooid.intValue());


    q = new DbOomQuery(sql("select $g.id+$b.id as total, $C{g.*}, $g.id*2 as gdub, $C{b.*}, $g.id/3.0, $g.name from $T{g}, $T{b} where $b.girlId=$g.id").
        aliasColumnsAs(COLUMN_CODE).use("b", Boy.class).use("g", Girl.class));
    list = q.list(Integer.class, Girl.class, Long.class, Boy.class, Float.class, String.class);
    assertEquals(1, list.size());
    result = (Object[]) list.get(0);
    assertEquals(6, result.length);
    assertEquals(4, ((Integer) result[0]).intValue());
    checkGirl3((Girl) result[1]);
    assertEquals(6, ((Long) result[2]).intValue());
    checkBoy((Boy) result[3]);

    assertEquals(1.0f, ((Float) result[4]).floatValue(), 0.05);
    assertEquals("Monica", result[5]);


    q = new DbOomQuery(sql("select $C{g.*}, $C{g.*} from $T{g} where $g.id=3").aliasColumnsAs(COLUMN_CODE).use("g", Girl.class));
    list = q.list(Girl.class, Girl.class);
    assertEquals(1, list.size());
    result = (Object[]) list.get(0);
    checkGirl3((Girl) result[0]);
    checkGirl3((Girl) result[1]);

    q = new DbOomQuery(sql("select $C{g.*}, $g.name from $T{g} where $g.id=3").aliasColumnsAs(COLUMN_CODE).use("g", Girl.class));
    list = q.list(Girl.class, String.class);
    assertEquals(1, list.size());
    result = (Object[]) list.get(0);
    checkGirl3((Girl) result[0]);
    assertEquals("Monica", result[1]);

    q = new DbOomQuery(sql("select $g.name, $C{g.*} from $T{g} where $g.id=3").aliasColumnsAs(COLUMN_CODE).use("g", Girl.class));
    list = q.list(String.class, Girl.class);
    assertEquals(1, list.size());
    result = (Object[]) list.get(0);
    checkGirl3((Girl) result[1]);
    assertEquals("Monica", result[0]);

    //q.reset();
    list = q.list(String.class, Girl.class);
    result = (Object[]) list.get(0);
    assertEquals("Monica", result[0]);


    q.close();

    // ---------------------------------------------------------------- finder

    girl = new Girl();
    girl.id = 1;
    badGirl = new BadGirl();
    badBoy = new BadBoy();

    DbOomQuery f = DbEntitySql.find(girl).aliasColumnsAs(null).query();
    f.setDebugMode();
    assertEquals("select Girl_.ID, Girl_.NAME, Girl_.SPECIALITY from GIRL Girl_ where (Girl_.ID=:girl.id)", f.toString());
    f.init();
    assertEquals("select Girl_.ID, Girl_.NAME, Girl_.SPECIALITY from GIRL Girl_ where (Girl_.ID=1)", f.toString());
    f.close();
    f = DbEntitySql.find(badGirl).aliasColumnsAs(null).query();
    f.setDebugMode();
    assertEquals("select BadGirl_.ID, BadGirl_.NAME, BadGirl_.SPECIALITY from GIRL BadGirl_ where (1=1)", f.toString());
    f.close();
    f = DbEntitySql.find(badBoy).aliasColumnsAs(null).query();
    f.setDebugMode();
    assertEquals("select BadBoy_.ID, BadBoy_.GIRL_ID, BadBoy_.NAME from BOY BadBoy_ where (1=1)", f.toString());
    f.close();

    girl.name = "Monica";
    badGirl.fooname = "Anna";
    badBoy.nejm = "David";

    f = DbEntitySql.find(girl).query();
    f.setDebugMode();
    f.init();
    assertEquals("select Girl_.ID, Girl_.NAME, Girl_.SPECIALITY from GIRL Girl_ where (Girl_.ID=1 and Girl_.NAME='Monica')", f.toString());
    f.close();
    f = DbEntitySql.find(badGirl).query();
    f.setDebugMode();
    f.init();
    assertEquals("select BadGirl_.ID, BadGirl_.NAME, BadGirl_.SPECIALITY from GIRL BadGirl_ where (BadGirl_.NAME='Anna')", f.toString());
    f.close();
    f = DbEntitySql.find(badBoy).query();
    f.setDebugMode();
    f.init();
    assertEquals("select BadBoy_.ID, BadBoy_.GIRL_ID, BadBoy_.NAME from BOY BadBoy_ where (BadBoy_.NAME='David')", f.toString());
    f.close();

    // ---------------------------------------------------------------- whole round

    badGirl = new BadGirl();
    badGirl.fooid = Integer.valueOf(2);
    f = DbEntitySql.findById(badGirl).query();
    list = f.autoClose().list(BadGirl.class);
    assertTrue(f.isClosed());
    assertEquals(1, list.size());
    checkBadGirl2((BadGirl) list.get(0));

    f = DbEntitySql.count(badGirl).query();
    count = (int) f.autoClose().executeCount();
    assertEquals(1, count);
    assertTrue(f.isClosed());


    DbSqlGenerator g = DbEntitySql.deleteById(badGirl);
    f = new DbOomQuery(g).autoClose();
    f.executeUpdate();
    assertTrue(f.isClosed());

    f = DbEntitySql.count(badGirl).query();
    count = (int) f.autoClose().executeCount();
    assertEquals(0, count);
    assertTrue(f.isClosed());

    badGirl.fooid = null;
    f = DbEntitySql.count(badGirl).query().autoClose();
    count = (int) f.executeCount();
    assertEquals(2, count);
    assertTrue(f.isClosed());

    girl = new Girl();
    girl.id = 1;
    girl.name = "A%";
    f = new DbOomQuery("select * from GIRL where id >= :girl.id and name like :girl.name");
    f.setDebugMode();
    f.setBean("girl", girl);
    assertEquals("select * from GIRL where id >= 1 and name like 'A%'", f.toString());
    count = (int) f.executeCount();
    assertEquals(1, count);

    //f.reset();
    girl.id = -2;
    f.setBean("girl", girl);
    assertEquals("select * from GIRL where id >= -2 and name like 'A%'", f.toString());
    count = (int) f.executeCount();
    assertEquals(1, count);

    //f.reset();

    badGirl = new BadGirl();
    badGirl.fooid = Integer.valueOf(3);
    f = DbEntitySql.findByColumn(BadBoy.class, "girlId", badGirl.fooid).query();
    f.setDebugMode();
    f.init();
    assertEquals("select BadBoy_.ID, BadBoy_.GIRL_ID, BadBoy_.NAME from BOY BadBoy_ where BadBoy_.GIRL_ID=3", f.toString());
    f.close();

    f = DbEntitySql.findForeign(BadBoy.class, badGirl).query();
    f.setDebugMode();
    f.init();
    assertEquals("select BadBoy_.ID, BadBoy_.GIRL_ID, BadBoy_.NAME from BOY BadBoy_ where BadBoy_.GIRL_ID=3", f.toString());

    f.close();

    badGirl = new BadGirl();
    badGirl.fooid = Integer.valueOf(3);
    BadGirl bbgg = DbEntitySql.findById(badGirl).query().find(BadGirl.class);
    bbgg.boys = DbEntitySql.findForeign(BadBoy.class, bbgg).query().list(BadBoy.class);

    assertNotNull(bbgg);
    assertEquals(3, bbgg.fooid.intValue());
    assertNotNull(bbgg.boys);
    assertEquals(1, bbgg.boys.size());
    assertEquals(1, bbgg.boys.get(0).ajdi.intValue());


    // ---------------------------------------------------------------- update

    badGirl = new BadGirl();
    badGirl.fooid = Integer.valueOf(1);
    badGirl = DbEntitySql.findById(badGirl).query().find(badGirl.getClass());
    checkBadGirl1(badGirl);

    badGirl.fooname = "Ticky";
    DbEntitySql.update(badGirl).query().executeUpdate();

    badGirl = new BadGirl();
    badGirl.fooid = Integer.valueOf(1);
    badGirl = DbEntitySql.findById(badGirl).query().find(badGirl.getClass());
    checkBadGirl1Alt(badGirl);

    badGirl.foospeciality = null;
    DbEntitySql.updateAll(badGirl).query().executeUpdate();

    badGirl = new BadGirl();
    badGirl.fooid = Integer.valueOf(1);
    badGirl = DbEntitySql.findById(badGirl).query().find(badGirl.getClass());
    checkBadGirl1Alt2(badGirl);


    // ---------------------------------------------------------------- joins

    q.close();


    // ---------------------------------------------------------------- double table names

    q = new DbOomQuery("select g.*, g.* from GIRL g order by g.ID");
    //noinspection unchecked
    List<Object[]> g2 = q.list(Girl.class, Girl.class);
    assertEquals(2, g2.size());
    Object[] g2o = g2.get(0);
    assertEquals(2, g2o.length);
    checkGirl1Alt((Girl) g2o[0]);
    checkGirl1Alt((Girl) g2o[1]);
    q.close();

    q = new DbOomQuery("select g.*, g2.* from GIRL g, GIRL g2 where g.ID=1 and g2.ID=3");
    //noinspection unchecked
    g2 = q.list(Girl.class, Girl.class);
    assertEquals(1, g2.size());
    g2o = g2.get(0);
    assertEquals(2, g2o.length);
    checkGirl1Alt((Girl) g2o[0]);
    checkGirl3Alt((Girl) g2o[1]);
    q.close();

    session.closeSession();

  }
View Full Code Here

  @Test
  public void testOrm2() {
    DbOomManager.resetAll();

    DbSession session = new DbThreadSession(cp);

    executeUpdate(session, "drop table BOY if exists");
    executeUpdate(session, "drop table GIRL if exists");

    String sql = "create table GIRL (" +
        "ID      IDENTITY," +
        "NAME    varchar(20)  not null," +
        "SPECIALITY  varchar(20)  null," +
        "TIME timestamp not null default CURRENT_TIMESTAMP, " +
        "primary key (ID)" +
        ')';

    DbQuery query = new DbQuery(sql);
    query.autoClose().executeUpdate();
    assertTrue(query.isClosed());

    executeUpdate(session, "insert into GIRL(ID, NAME, SPECIALITY) values(1, 'Anna', 'swim')");
    executeUpdate(session, "insert into GIRL(ID, NAME, SPECIALITY) values(2, 'Sandra', 'piano')");
    executeUpdate(session, "insert into GIRL(NAME) values('Monica')");

    session.closeSession();
    assertTrue(session.isSessionClosed());


    // prepare
    session = new DbThreadSession(cp);


    // ---------------------------------------------------------------- girls

    DbOomQuery q = new DbOomQuery("select * from GIRL where ID=1");

    Girl girl = q.find(Girl.class);
    checkGirl1(girl);
    assertTrue(q.isActive());

    IdName idName = q.find(IdName.class);
    assertNotNull(idName);
    assertEquals(1, idName.id);
    assertEquals("Anna", idName.name);

    try {
      q.find();        // this will fail since no entity is registered!
      fail();
    } catch (DbOomException doex) {
      // ignore
    }

    assertEquals(2, DbOomManager.getInstance().getTotalTypes());
    assertEquals(0, DbOomManager.getInstance().getTotalTableNames());
    assertEquals(2, DbOomManager.getInstance().getTotalNames());

    DbOomManager.getInstance().registerEntity(Girl.class, true);
    girl = q.find();
    checkGirl1(girl);

    assertEquals(2, DbOomManager.getInstance().getTotalTypes());
    assertEquals(1, DbOomManager.getInstance().getTotalTableNames());
    assertEquals(2, DbOomManager.getInstance().getTotalNames());

    q.close();

    session.closeSession();


    /**
     * Test fails on HSQLDB 1.8 since generated columns are not supported.
     */
    session = new DbThreadSession(cp);

    q = new DbOomQuery("insert into GIRL (NAME) values('Janna')");
    q.setGeneratedColumns();
    q.executeUpdate();
    long key = q.getGeneratedKey();
    assertEquals(4, key);
    q.close();

    q = new DbOomQuery("insert into GIRL (NAME) values('Janna2')");
    q.setGeneratedColumns("ID", "TIME");
    q.executeUpdate();
    Long Key = q.findGeneratedKey(Long.class);
    assertEquals(5, Key.longValue());
    q.close();

    q = new DbOomQuery("insert into GIRL (NAME) values('Sasha')");
    q.setGeneratedColumns("ID, TIME");
    q.executeUpdate();
    ResultSet rs = q.getGeneratedColumns();
    assertEquals(1, q.getOpenResultSetCount());
    try {
      while (rs.next()) {
        int id = rs.getInt(1);
        assertEquals(6, id);
        try {
          rs.getInt(2);
          fail();
        } catch (SQLException sex) {
          // ignore
        }
      }
    } catch (SQLException sex) {
      fail(sex.getMessage());
    }
    q.closeResultSet(rs);
    q.close();
    assertEquals(0, q.getOpenResultSetCount());

    session.closeSession();


    session = new DbThreadSession(cp);
    DbOomManager.getInstance().registerEntity(Girl2.class, true);
    Girl2 g2 = new Girl2("Gwen");
    q = DbEntitySql.insert(g2).query();
    assertEquals("insert into GIRL (NAME) values (:girl2.name)", q.getQueryString());
    q.setGeneratedColumns("ID");
    q.executeUpdate();
    DbOomUtil.populateGeneratedKeys(g2, q);
    //g2.id = Integer.valueOf((int) q.getGeneratedKey());
    q.close();
    assertEquals(7, g2.id.intValue());

    g2 = DbEntitySql.findByColumn(Girl2.class, "name", "Gwen").query().find(Girl2.class);
    assertEquals("Gwen", g2.name);
    assertNull(g2.speciality);
    assertNotNull(g2.time);
    assertEquals(7, g2.id.intValue());

    session.closeSession();


    session = new DbThreadSession(cp);

    q = DbSqlBuilder.sql("select * from $T{Girl g} where $g.name like :what order by $g.id").query();
    q.setString("what", "%anna%");
    List<Girl2> girls = q.list(Girl2.class);
    assertEquals(2, girls.size());
    checkGirl4(girls.get(0));
    checkGirl5(girls.get(1));
    q.close();

    session.closeSession();
  }
View Full Code Here

  ParsedSql q6;


  @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();
  }
View Full Code Here

    dbOom.registerEntity(Enumerator.class);
  }

  @Test
  public void testEnums() {
    DbSession session = new DbThreadSession(cp);

    String sql = "create table ENUMERATOR(ID int, NAME varchar(20), STATUS int)";

    DbQuery query = new DbQuery(sql);
    query.executeUpdate();

    Enumerator e = new Enumerator();
    e.id = 2;
    e.name = "Ikigami";
    e.status = Enumerator.STATUS.ONE;

    DbSqlGenerator gen = insert(e);
    query = new DbOomQuery(gen);
    query.executeUpdate();

    session.closeSession();
  }
View Full Code Here

    dbOom.registerEntity(Bean1.class);
  }

  @Test
  public void testMappingNoTable() {
    DbSession session = new DbThreadSession(cp);

    assertEquals(1, DbEntitySql.insert(new Girl(1, "Anna", "swim")).query().autoClose().executeUpdate());
    assertEquals(0, session.getTotalQueries());

    // one
    DbOomQuery q = new DbOomQuery(DbSqlBuilder.sql("select $C{g.id} + 10, UCASE($C{g.name}) from $T{Girl g}"));
    Object[] row = q.find(Integer.class, String.class);

    assertEquals(Integer.valueOf(11), row[0]);
    assertEquals("ANNA", row[1]);


    // two
    DbSqlBuilder dbSqlBuilder = DbSqlBuilder
        .sql("select $g.id + 10 as $C{Bean1.sum}, UCASE($g.name) as $C{Bean1.bigName}, $C{g.*} from $T{Girl g} where $g.id=1")
        .aliasColumnsAs(ColumnAliasType.COLUMN_CODE);

    assertEquals(
        "select g.ID + 10 as col_0_, UCASE(g.NAME) as col_1_, g.ID as col_2_, g.NAME as col_3_, g.SPECIALITY as col_4_ from GIRL g where g.ID=1",
        dbSqlBuilder.generateQuery());

    dbSqlBuilder.reset();

    assertEquals(
        "select g.ID + 10 as Bean1$SUM, UCASE(g.NAME) as Bean1$BIG_NAME, g.ID, g.NAME, g.SPECIALITY from GIRL g where g.ID=1",
        dbSqlBuilder.generateQuery());

    dbSqlBuilder.reset();

    q = new DbOomQuery(dbSqlBuilder);
    row = q.find(Bean1.class, Girl.class);

    Bean1 bean1 = (Bean1) row[0];
    Girl girl = (Girl) row[1];

    assertNotNull(bean1);
    assertEquals(11, bean1.getSum().intValue());
    assertEquals("ANNA", bean1.getBigName());

    assertNotNull(girl);
    assertEquals("Anna", girl.name);
    assertEquals("swim", girl.speciality);
    assertEquals(1, girl.id);


    // three
    dbSqlBuilder = DbSqlBuilder.sql(
        "select $g.id + 10 as $C{Bean1.sum}, UCASE($g.name) as $C{Bean1.bigName}, $C{g.*} from $T{Girl g} where $g.id=1");
    assertEquals(
        "select g.ID + 10 as Bean1$SUM, UCASE(g.NAME) as Bean1$BIG_NAME, g.ID, g.NAME, g.SPECIALITY from GIRL g where g.ID=1",
        dbSqlBuilder.generateQuery());

    dbSqlBuilder.reset();

    q = new DbOomQuery(dbSqlBuilder);
    row = q.find(Bean1.class, Girl.class);

    bean1 = (Bean1) row[0];
    girl = (Girl) row[1];

    assertNotNull(bean1);
    assertEquals(11, bean1.getSum().intValue());
    assertEquals("ANNA", bean1.getBigName());

    assertNotNull(girl);
    assertEquals("Anna", girl.name);
    assertEquals("swim", girl.speciality);
    assertEquals(1, girl.id);

    session.closeSession();
  }
View Full Code Here

    DbOomManager.resetAll();
  }

  @Test
  public void testMapping() throws SQLException {
    DbSession session = new DbThreadSession(cp);

    executeUpdate(session, "drop table FOO if exists");
    String sql = "create table FOO (" +
        "ID      integer    not null," +
        "NUMBER    integer   not null," +
        "STRING    integer    not null," +
        "STRING2  integer    not null," +
        "BOO    integer    not null," +
        "COLOR    varchar(50)    not null," +
        "WEIGHT    integer    not null," +
        "TIMESTAMP  timestamp  not null," +
        "TIMESTAMP2  timestamp  not null," +
        "CLOB    longvarchar  not null," +
        "BLOB    longvarbinary not null," +
        "DECIMAL  real    not null," +
        "DECIMAL2  varchar(50)    not null," +
        "JDT1    bigint    not null," +
        "JDT2    varchar(50)    not null," +
        "primary key (ID)" +
        ')';
    executeUpdate(session, sql);

    sql = "insert into FOO values (1, 555, 173, 7, 999, 'red', 1, '2009-08-07 06:05:04.3333', '2010-01-20 01:02:03.4444', 'W173', 'ABCDEF', 1.01, '-7.17', 0, '0')";
    executeUpdate(session, sql);

    DbOomManager dbOom = DbOomManager.getInstance();
    dbOom.registerEntity(Foo.class);
    SqlTypeManager.register(Boo.class, BooSqlType.class);
    SqlTypeManager.register(FooWeight.class, FooWeigthSqlType.class);

    List<Foo> foos = new DbOomQuery("select * from FOO").list(Foo.class);
    assertEquals(1, foos.size());
    Foo foo = foos.get(0);
    assertEquals(1, foo.id);
    assertEquals(555, foo.number.value);
    assertEquals("173", foo.string);
    assertEquals("7", foo.string2);
    assertEquals(999, foo.boo.value);
    assertEquals(FooColor.red, foo.color);
    assertEquals(FooWeight.light, foo.weight);
    assertNotNull(foo.timestamp);
    assertEquals(109, foo.timestamp.getYear());
    assertEquals(6, foo.timestamp.getHours());
    assertEquals(5, foo.timestamp.getMinutes());
    assertNotNull(foo.timestamp2);
    assertEquals(2010, foo.timestamp2.getYear());
    assertEquals(1, foo.timestamp2.getHour());
    assertEquals(2, foo.timestamp2.getMinute());
    assertNotNull(foo.clob);
    assertEquals(4, foo.clob.length());
    assertEquals("W173", foo.clob.getSubString(1, 4));
    assertEquals(3, foo.blob.length());
    assertEquals((byte) 0xAB, foo.blob.getBytes(1, 3)[0]);
    assertEquals((byte) 0xCD, foo.blob.getBytes(1, 3)[1]);
    assertEquals((byte) 0xEF, foo.blob.getBytes(1, 3)[2]);
    assertEquals("1.01", foo.decimal.toString().substring(0, 4));
    assertEquals("-7.17", foo.decimal2.toString().substring(0, 5));
    assertEquals("1970-01-01", foo.jdt1.toString("YYYY-MM-DD"));
    assertEquals("1970-01-01", foo.jdt2.toString("YYYY-MM-DD"));

    foo.string = "371";
    foo.string2 = "007";
    foo.boo.value = 213;
    foo.color = FooColor.yellow;
    foo.weight = FooWeight.heavy;
    foo.number.value = 222;
    foo.timestamp.setYear(108);
    foo.decimal = new Double("34.12");
    foo.decimal2 = new BigDecimal("1.099");
    DbOomQuery doq = new DbOomQuery(DbEntitySql.update(foo));
    foo.jdt1.setDay(2);
    foo.jdt1.setYear(3000);
    foo.jdt2.setDay(3);
    foo.jdt2.setYear(2900);
    doq.executeUpdate();


    doq = new DbOomQuery(DbEntitySql.updateColumn(foo, "timestamp2", new JDateTime("2010-02-02 20:20:20.222")));

    doq.executeUpdate();

    foos = new DbOomQuery("select * from FOO").list(Foo.class);
    assertEquals(1, foos.size());
    foo = foos.get(0);
    assertEquals(1, foo.id);
    assertEquals("371", foo.string);
    assertEquals("7", foo.string2);
    assertEquals(213, foo.boo.value);
    assertEquals(222, foo.number.value);
    assertEquals(FooColor.yellow, foo.color);
    assertEquals(FooWeight.heavy, foo.weight);
    assertEquals(108, foo.timestamp.getYear());
    assertEquals(2010, foo.timestamp2.getYear());
    assertEquals(20, foo.timestamp2.getHour());
    assertEquals(20, foo.timestamp2.getMinute());
    assertEquals(4, foo.clob.length());
    assertEquals("W173", foo.clob.getSubString(1, 4));
    assertEquals(3, foo.blob.length());
    assertEquals("34.12", foo.decimal.toString());
    assertEquals("1.099", foo.decimal2.toString().substring(0, 5));
    assertEquals("3000-01-02", foo.jdt1.toString("YYYY-MM-DD"));
    assertEquals("2900-01-03", foo.jdt2.toString("YYYY-MM-DD"));

    executeUpdate(session, "drop table FOO if exists");
    session.closeSession();
  }
View Full Code Here

    dbOom.registerEntity(Boy.class);
  }

  @Test
  public void testAppDao1() {
    DbSession session = new DbSession(cp);
    ThreadDbSessionHolder.set(session);

    GenericDao dao = new GenericDao();

    // save

    Girl girl = new Girl();
    girl.setName("Emma");
    girl.setSpeciality("piano");
    girl.setId(Long.valueOf(1));

    dao.save(girl);
    assertEquals(1, girl.getId().longValue());

    Boy boy1 = new Boy();
    boy1.setId(1);
    boy1.setName("Oleg");
    boy1.setGirlId(1);

    Boy boy2 = new Boy();
    boy2.setId(2);
    boy2.setName("Marco");
    boy2.setGirlId(1);

    ArrayList<Boy> boys = new ArrayList<Boy>();
    boys.add(boy1);
    boys.add(boy2);

    dao.saveAll(boys);

    // find

    Boy dbBoy = dao.findById(Boy.class, 1);
    assertEquals(boy1.getId(), dbBoy.getId());
    assertEquals(boy1.getName(), dbBoy.getName());
    assertEquals(boy1.getGirlId(), dbBoy.getGirlId());

    // update

    girl.setSpeciality("Guitar");
    dao.update(girl);

    long count = dao.count(Girl.class);
    assertEquals(1, count);

    Girl dbGirl = dao.findById(Girl.class, 1);
    assertEquals("Guitar", dbGirl.getSpeciality());
    assertEquals("Emma", dbGirl.getName());

    // update property

    dao.updateProperty(girl, "speciality", "math");

    dbGirl = dao.findById(Girl.class, 1);
    assertEquals("math", dbGirl.getSpeciality());
    assertEquals("math", girl.getSpeciality());
    assertEquals("Emma", dbGirl.getName());

    // add one more girl
    Girl girl2 = new Girl();
    girl2.setName("Lina");
    girl2.setSpeciality("crazy");
    girl2.setId(Long.valueOf(2));

    dao.save(girl2);
    count = dao.count(Girl.class);
    assertEquals(2, count);

    Girl emma = dao.findOneByProperty(Girl.class, "name", "Emma");
    assertNotNull(emma);
    assertEquals(1, emma.getId().longValue());

    Girl none = dao.findOneByProperty(Girl.class, "name", "Www");
    assertNull(none);

    // find by matching

    Girl match = new Girl();
    match.setName("Emma");
    dbGirl = dao.findOne(match);

    assertEquals(emma.getId(), dbGirl.getId());
    assertEquals(emma.getName(), dbGirl.getName());
    assertEquals(emma.getSpeciality(), dbGirl.getSpeciality());

    Boy boyMatch = new Boy();
    boyMatch.setGirlId(1);

    List<Boy> dbBoys = dao.find(boyMatch);
    assertEquals(2, dbBoys.size());

    // find by separate matching class

    boyMatch = new Boy();
    boyMatch.setName("Oleg");
    dbBoys = dao.find(Boy.class, boyMatch);
    assertEquals(1, dbBoys.size());

    // correct way

    BoyCriteria boyCriteria = new BoyCriteria();
    boyCriteria.setName("Oleg");

    dbBoys = dao.find(Boy.class, boyCriteria);
    assertEquals(1, dbBoys.size());


    // list all

    dbBoys = dao.listAll(Boy.class);
    assertEquals(2, dbBoys.size());

    // related

    dbBoys = dao.findRelated(Boy.class, emma);
    assertEquals(2, dbBoys.size());


    // delete

    dao.deleteById(Boy.class, 1);
    dao.deleteById(boy2);

    count = dao.count(Boy.class);
    assertEquals(0, count);

      // delete all

    List<Girl> girls = dao.listAll(Girl.class);
    dao.deleteAllById(girls);

    count = dao.count(Girl.class);
    assertEquals(0, count);

    session.closeSession();
    ThreadDbSessionHolder.remove();
  }
View Full Code Here

TOP

Related Classes of jodd.db.DbSession

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.