executeUpdate oddity

View: New views
1 Messages — Rating Filter:   Alert me  

executeUpdate oddity

by Eric Lambrecht :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've got a simple domain object called Junk:

class Junk {
   String someText;
}

.. and a simple service called JunkService:

class JunkService {

    boolean transactional = true

    def serviceMethod() {
      Junk junk = new Junk(someText: "this is some text");
      junk.save(flush:true);

      Junk.executeUpdate("update Junk set someText = 'changed'");
    }
}

I thought that after that call to junkService.serviceMethod() I'd have a single 'junk' entry in the database with 'changed' as its value for 'someText', but in fact it doesn't update the new row I just threw in the database. I wrote this integration test:

class JunkTests extends GrailsUnitTestCase {

    def junkService

    void testSomething() {
      junkService.serviceMethod();

      def entries = Junk.findAll();

      assertEquals(1, entries.size());
      assertEquals("changed", entries[0].someText);
    }
}

and it always fails on the second assertion.

Is there something obvious that I'm missing? Are 'executeUpdate' methods run in a different session or transaction? Is there some extra flush() call I've got to make? I've run this against the HSQLDB in-memory database and MySQL, with the same results for each database.

I know I could do a findAll query and run the update on the individual instances, but in my actual code I'll be updating a huge number of rows and would really like to use the DML to keep it efficient.

I'm running Grails 1.1.1 on Snow Leopard on a Mac.

Eric...