After issuing an DBQuery with an insert, how can I get the generated id, so I can add it back to the DAO?

48 views Asked by At

Its more than just insert, really. If I already have a partially loaded DAO, how can I load the rest of it?

What I'm going to do is to do a select query, and then use BeanCopy. I'd rather have the result set mapper directly set the properties on the DAO.

1

There are 1 answers

0
igr On

Ok, let me try to answer this. For all generated values (like auto-generated IDs) you can use the following flow:

    q = DbEntitySql.insert(foo).query();
    // ... or any other way to get DbQuery

    q.setGeneratedColumns("ID");
    q.executeUpdate();
    DbOomUtil.populateGeneratedKeys(dao, q);

Basically, for each query/dao you need to specify fields that are autogenerated. Currently there is no annotation for doing so - we are trying to keep number of annotations small as possible. We are working on making this more automatic.

Now, for populating the DAO. I would not use BeanCopy - simply load new DAO instance and ditch the old one. So after you execute the full select query you will get the full DAO loaded, and just continue with it.