Re: SQLTemplate with SQL MAX function

From: Evgeny Ryabitskiy (evgeny.ryabitski..mail.com)
Date: Mon Sep 28 2009 - 11:08:59 EDT

  • Next message: Aristedes Maniatis: "Re: Prefetching has no effect"

    Hello!

    To solve problem with key in map you can use aliases:

    String sql = "SELECT max(ordering) as MAX_ORDERING FROM user_bookmark WHERE
    userid=$uid");
    SQLTemplate template = new SQLTemplate(UserBookmark.class, sql);

    Now map contains key "MAX_ORDERING"

    You can write you query to cayenne DomainMap.map.xml and use NamedQuery that
    returns List of DataRows.

    Unfortunately you can't use SQLTemplate without DataObject, it's very not
    comfortable cause sometimes there is no need in mapping while forming
    queries at runtime... Hope it will be fixed...

    Best regards,
      Evgeny Ryabitskiy.

    2009/9/28 Borut Bolčina <borut.bolcin..mail.com>:
    > Hi,
    >
    > Is there a nicer way of executing
    > SELECT max(ordering) FROM user_bookmark WHERE userid=293130
    >
    > then this:
    > SQLTemplate template = new SQLTemplate(UserBookmark.class, "SELECT
    > max(ordering) FROM user_bookmark WHERE userid=$uid");
    > template.setParameters(Collections.singletonMap("uid", user.getId()));
    > template.setFetchingDataRows(true);
    > List<UserBookmark> res = context.performQuery(template);
    >
    > Integer maxOrdering = null;
    > if (res != null) {
    > Map row = (Map)res.get(0);
    > if (row != null) {
    > maxOrdering = (Integer) row.get("max(ordering)");
    > }
    > }
    > if (maxOrdering == null) {
    > maxOrdering = 0;
    > }
    >
    > userBookmark.setOrdering(maxOrdering + 1);
    >
    > I took me some time to figure out the name of the key in the map. Note
    that
    > userid is a FK, so it is not mapped in the modeler.
    >
    > Regards,
    > Borut
    >



    This archive was generated by hypermail 2.0.0 : Mon Sep 28 2009 - 11:09:20 EDT