SQLTemplate with SQL MAX function

From: Borut Bolčina (borut.bolcin..mail.com)
Date: Mon Sep 28 2009 - 05:31:12 EDT

  • Next message: Evgeny Ryabitskiy: "Re: SQLTemplate with SQL MAX function"

    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 - 05:31:26 EDT