Ordering Results with JDBI

624 views
Skip to first unread message

tim.gr...@seekersolutions.com

unread,
May 27, 2013, 7:06:03 PM5/27/13
to jd...@googlegroups.com
I have an on-demand DAO like the following:


@RegisterMapper(BookMapper.class)
public interface BookDAO {
    @SqlQuery("select * from books order by :order_by asc limit :limit")
    public List<Book> getBooks(@Bind("order_by") String orderBy, @Bind("limit") int limit);
}

for some reason, though, the objects coming out appear to not be ordered by the orderBy parameters. When I change the above to


@RegisterMapper(BookMapper.class) 
public interface BookDAO {
    @SqlQuery("select * from books order by author asc limit :limit") 
    public List<Book> getBooks(@Bind("limit") int limit);
}

then the query returns an ordered list as expected. For some reason, the binding on Limit also works properly. What is special about binding on the order by clause? Is there a way to specify the ordering like this?

Brian McCallister

unread,
May 27, 2013, 7:36:30 PM5/27/13
to jd...@googlegroups.com, jd...@googlegroups.com
I believe you are ordering by the value "author" not by the field author. If you want a dynamic order by you probably need to template it in rather then bind it. If you do template, looks at the stringtemplate3 locator, it assumes SQL literal if no template group, etc.
--
You received this message because you are subscribed to the Google Groups "jDBI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+uns...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

tim.gr...@seekersolutions.com

unread,
May 28, 2013, 4:57:02 PM5/28/13
to jd...@googlegroups.com
Thanks for the quick reply! I'll look into this shortly!
Reply all
Reply to author
Forward
0 new messages