Re: SQLTemplate with SQL MAX function

From: Borut Bolčina (borut.bolcin..mail.com)
Date: Tue Sep 29 2009 - 16:14:57 EDT


2009/9/28 Evgeny Ryabitskiy <evgeny.ryabitski..mail.com>

> 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"
>
>
Thanks, makes sense.

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

Unfortunately there is an empty page at
http://cayenne.apache.org/doc/modeling-queries.html

>
> 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...
>

And that holds true for EJBQLQuery as well does it not? Maybe there should
be an advice somewhere in the docs that one must use DataRows as soon as
there is just one non-mapped attribute in the query.

I would also recomend a FAQ section on the main page which would answer
common questions, like "How do I do SELECT count(*) FROM artist WHERE
first_name LIKE '%abl%'

I would also report a broken link on the front page for the Wiki (
http://cayenne.apache.org/CAY/index.html)

> 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 : Tue Sep 29 2009 - 16:15:19 EDT