Saturday, April 28, 2012

Keyword column names with Hibernate

I ran in to an interesting problem today, and I thought I might as well help pass it around. I was attempting to create a new Hibernate class for a new table I had created. One of the column names for this table was "index" - which, unfortunately, is a keyword in MySQL. So the Hibernate code I wrote looked something like this:

    @Column(name="index")
    public int getIndex() {
        return index;
    }

When I attempted to run my new code, I was getting a very uninformative error attempting to write to that table:

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index, blahblahotherstufffrommyquery... ' at line 2

Excellent. Perfectly clear. Yeah.

After turning on show sql and going over to MySQLWorkbench, I found that I was getting an error there too - as it turns out, the column "index" needed to be escaped with back ticks, "`index`". Why Hibernate does not do this by default, I don't know. Luckily, as it turns out, it is a fairly easy fix - just add the back ticks to the column name in the annotation, like such:

    @Column(name="`index`")
    public int getIndex() {
        return index;
    }

This makes things work much easier. Just something to bear in mind for future reference.

Friday, April 20, 2012

Moving a jQuery Date Picker

In case you haven't noticed by this point, I am a jQuery fan. It makes working with javascript about a million times easier. Every once in a while, I stumble upon a little quirk, though, that ends up sinking hours of time trying to track down. This is one of those quirks.

I needed to make use of the jQuery UI Date Picker feature. Lovely little tool for dropping in an easy date picker. However, I was also doing some other jQuery magic with it - the date picker was in a form that could be used to edit multiple different instances on the page. I had it start out as hidden, then move it to the appropriate location and show it. Here is the code:
function moveEditForm(newLocation) {
    $('#my-edit-form').remove().insertAfter(newLocation).show();
}

There was one problem - the date picker, which I initialized on page load, would no longer open up the calendar. Took me forever to figure out why, as there was no javascript errors, nothing at all to indicate a problem. As it turns out, if you remove an initialized date picker from the page, and then add it back in somewhere else, the date picker functionality gets all foobarred.

With a little experimentation, I found that what I had to do was destroy the date picker before removing it from the page, then re-init it after I inserted it back in. The updated code looked more like this:
function moveEditForm(newLocation) {
    $( "#my-datepicker" ).datepicker("destroy");
    $('#my-edit-form').remove().insertAfter(newLocation).show();
    $( "#my-datepicker" ).datepicker();
}

I don't know why it messes up when that happens, but regardless, this patches it back up again.

Tuesday, April 17, 2012

Just a quick update

Once again, an extraordinary amount of time has passed since my last post. This time, though, I have a really good excuse - in that time, I have switched jobs, and it's taken a while for me to get back into a normal pattern. I am still developing in Java, but now solely as a web service backend, working with a flex frontend.

Learning to code in flex has been very interesting to say the least. Some of the things that are so annoying to get right in other platforms are just an easy, simple tag in flex. Of course, as with anything else, there are drawbacks. There can be times when something messes up in flex for no particular reason, and it takes forever to track down, eating up a good chunk of the productivity gained from the easy nature of flex. Also, there is some concern about the portability to other platforms, though we have not experienced that as of yet. So far, I'm fairly happy with the new processes.

One big change with this new job is that I have switched away from using Eclipse, and I now use Intellij IDEA full time. The difference in my productivity has been remarkable. IDEA is not free, which is a big drawback compared to Eclipse, but in my mind the cost is easily worth it. Particularly for development in multiple languages - IDEA intelligently handles Java, Flex, PHP and Javascript practically out of the box, with all the normal IDE bells and whistles, such as code completion, go to function definition, etc. Eclipse can do these things, but it always seemed to take a huge amount of effort to get it to work properly with any language other than Java. If you've never tried IDEA, I highly recommend you go give it a shot. They have a 30 day free trial.

Anyway, that's the state of things. Hopefully I can get back into writing a few more posts now and then. Thanks for reading!