Some Java / MySQL IBATIS gotchas
Posted: 20091022
Wow. I had a funny day. First of all, I was happy to be back in the
eclipse java world, as compared to grails (Intellij). Secondly,
discovering iBATIS was an eye-opener, especially with regard to
startup times for integration tests (I use JUnit4 for this). Compared
to anything hibernate based... f.e. grails (grails has its own merits
anyway).
iBATIS has a cool plugin called iBATOR. It analyzes an existing table
structure, and creates CRUD DAO classes you can use, among with the
iBATIS SQL map file and the Java Pojo domain models themselves. And
the DAO classes have cool features such as Criteria classes which
enable you to query by example like this:
BlogEntryExample example = new BlogEntryExample();
example.createCriteria().andAuthorIsLike('%Amanjit%').andYearIsEqualTo(2007L);
List<BlogEntry> entries = dao.selectByExample(example);
which will return all Blog Entries from an author like "Amanjit" in
the year 2007. Nice stuff. Of course, the BlogEntry table (could be
called TABLE_BLOG_ENTRY) contains the columns called year and
author. Generated query class are huge but the feature is useful. The
whole stuff is autogenerated. you also get update, delete and create
DAO methods out of the box. You can have custom SQLMap entries in a
file, on regenerating from a changed DB model iBATIS won't override
your queries / resultmap config. Nice.
Of course, hand crafted hand optimized SQL is one of the main reasons
people use IBATIS. But, I have to say, you are still free to do
anyway, and regarding the 1:n, m:n joining stuff you probably have to
:-)
(or lets say does not work for my use case)
MySQL does not accept table alias names in DELETE statements. This is actually
not a gotcha, but a WTF. :-/
DELETE FROM BlogEntry WHERE year=2007 # works
DELETE FROM BlogEntry table_blog WHERE table_blog.year=2007 # does not work. toasted.
I created aliases so I can join tables with iBATIS without any problems
Basically, ibator generated DML for delete statements do not
work. Wow. I think PostgreSQL ..... Whatever don't get me started
:-). Anyway iBATOR has a plugin architecture so someone wrote a
plugin to solve this problem.
There is a funny thing about ibator. A setting in its configuration file
has a different meaning, depending on whether ibator is invoked from the
command line or from within eclipse. See the documentation for the config
setting targetProject:
"When running in the Eclipse environment, this specifies the
project and source folder where the objects will be saved. In
other environments, this value should be an existing directory on
the local file system."
Needless to say, this is a real WTF. Because the root of your
sourcecode package is normaly not the root of an eclipse project (we
use maven2 and eclipse:eclipse etc)...
YEAH. I guess I should have known that. Do your homework, dude ;-)
Anyway I spent nearly half an hour trying to figure out why my plugin
did not load.
So, it feels good to be back with eclipse. :-)